J2EE EJB with Vaadin Web Application

Hi all,

i’m very new to web application and vaadin. i’'m trying to create a stand alone application together with ejb module and also a web application with vaardin. i have created a web application with net beans maven following the tutorial and it works fine.
but i can seem to inject the ejb moduble into the application.

the errors are:

WELD-001408: Unsatisfied dependencies for type MySession with qualifiers @Default

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>MyUIServlet</servlet-name>
        <servlet-class>com.mycompany.vaadin1.MyUIServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyUIServlet</servlet-name>
        <url-pattern>/MyUIServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

MyUI.java
@Theme(“mytheme”)
public class MyUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();

    final TextField name = new TextField();
    name.setCaption("Type your name here:");

    Button button = new Button("Click Me");
    button.addClickListener(e -> {
        layout.addComponent(new Label("Thanks " + name.getValue()
                + ", it works!" + getSession1().sayHi()));
    });

    layout.addComponents(name, button);

    setContent(layout);
}

public MySession getSession1() {
    return ((MyUIServlet) VaadinServlet.getCurrent()).getSession();
}

}

MyUIServlet.java
@WebServlet(urlPatterns = “/*”, name = “MyUIServlet”, asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public class MyUIServlet extends VaadinServlet {

@Inject
private MySession mySession;

public MySession getSession() {
    return mySession;
}

}

please help me…

You need to remove MyUIServlet. CDI add-on will add VaadinCDIServlet automatically. Unless you are configuring multilple UI’s you do not need serlvet part in your app.

i think my ejb is in a another ejb module hence i cannot inject them?

i did this and got it working.

@CDIUI(“”)
@Theme(“mytheme”)
public class MyUI extends UI {

MySessionRemote mySession = lookupMySessionRemote();


@Override
protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();

    final TextField name = new TextField();
    name.setCaption("Type your name here:");

    Button button = new Button("Click Me");
    button.addClickListener(e -> {
        layout.addComponent(new Label("Thanks " + name.getValue()
                + ", it works!" + lookupMySessionRemote().sayHi()));
    });

    layout.addComponents(name, button);

    setContent(layout);
}

private MySessionRemote lookupMySessionRemote() {
    try {
        Context c = new InitialContext();
        return (MySessionRemote) c.lookup("ejb/MySession");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

}

Where is that bean defined MySession? Seems that its not recognized by container. Does this module have a META-INF/bean.xml ? Which annotations are on MySession?

i have created a ejb module with a class my session.
i have also create a remote interface and added into the web app module.

@DeclareRoles({“Sales”, “Admin”, “Production”, “Logistics”, “Designer”, “Assembly”, “Press”, “Trim”, “CTP”, “Driver”, “DTP”})
@Stateless
@Named
public class MySession implements MySessionRemote {

@Resource
private SessionContext context;

@Override
public String sayHi() {
    System.out.println("in SayHi");
    String userName2 = context.getCallerPrincipal().getName();
    System.out.println("Usernme: " + userName2);
    System.out.println("User in role: regular_role " + context.isCallerInRole("Admin"));
    System.out.println("User in rolse: member " + context.isCallerInRole("Admin"));
    return "Hihi";
}

}

i have attahced some photos of it.

41503.png
41504.png

You sure about your setup? Do you really need Remote interface to you ejb? I am afraid this is a wrong forum for your question.

  1. change @Inject to @EJB i am not sure if inject can work with remote EJBs. Also try to specify jndi property in the EJB.
  2. check the JNDI if EJB is registered there(logs probably)
  3. change this EJB to local and check if it works

Peasefull advises:

  1. do not overcomplicate things, problably you dont need EAR, all those dreams about sharing ejbs between projects are not comming through, accessing EJB remotely is a failure, its better to expose your domain objects over REST.
  2. after you realized you dont need EAR assemble simple war. with your local EJBs