Using Vaadin with Java EE 5/6

Hi all,

I’m curious about how to access an EJB (v3.0 or 3.1) within a Vaadin application. My thinking is that there is no point to using stateful session beans with Vaadin since the entire state of the application is already stored within the session. For instance, using the typical shopping cart example that spans X number of pages, there is no need to make a call to the server each time the user clicks “Next” to move to the next page. The whole state of the hypothetical shopping cart is saved in memory and one call can be made to submit it to the server once all the user’s information has been entered. To put it another way, there’s no point tying up a resource in the server for the session besides what’s already being stored by Vaadin. Does this sound correct to you?

A more specific question:

So far when writing a Vaadin application, I’ve looked up an EJB as needed like this with a simple helper class. The helper class BeanLocator performs the jndi lookup and casts the result for me. Only the init() method is shown here:


    @Override
    public void init() {
        TestBean bean = BeanLocator.locate(TestBean.class,
            "java:global/example");
        Window mainWindow = new Window("Hello World Application");
        Label label1 = new Label("Hello " + bean.hello("abc"));
        mainWindow.addComponent(label1);
        setMainWindow(mainWindow);
    }

In an event handler, I do the same thing to grab my service bean to make a call to the back end. However, following the
Hello GlassFish 3
example, I see a potentially simpler way to do this:


public class VaadinEEApp extends Application {

    private TestBean bean;

    public VaadinEEApp(TestBean bean) {
        this.bean = bean;
    }
    
    @Override
    public void init() {
        Window mainWindow = new Window("Hello World Application");
        Label label1 = new Label("Hello " + bean.hello("abc"));
        mainWindow.addComponent(label1);
        setMainWindow(mainWindow);
    }

    // this part is specific to EE6, but could be accomplished in EE5 with web.xml
    @WebServlet(urlPatterns = "/*")
    public static class Servlet extends AbstractApplicationServlet {

        @EJB
        private TestBean testBean;

        @Override
        protected Class<? extends Application> getApplicationClass() {
            return VaadinEEApp.class;
        }

        @Override
        protected Application getNewApplication(HttpServletRequest request)
            throws ServletException { return new VaadinEEApp(testBean); }
    }
}

Now this may be breaking all kinds of EJB laws; I’m not sure yet. :smiley: I’d like to run this by our EJB expert to see if this is ok or not. It works when I try it, but I’m not putting any kind of concurrent load on it.

So my question: is it correct to say that the ‘bean’ reference in my VaadinEE6App is stored within the servlet session for the life of the application? I’d like to make sure I understand the Vaadin side of things before I go asking about the correctness of the EJB usage. Doing the above makes it seem like the EJB could be accessed concurrently if I’m using the same instance in event handlers, but since the instance is really a proxy it may all be ok in the container (after all, Servlets are multithreaded and store an EJB field this way).

Thanks for your time, especially if you’ve made it this far through my convoluted example.

Cheers,
Bobby

For what it’s worth, I found out that the example above is ok from a Java EE perspective. Specifically, “Java EE requires that any EJB reference can be stored in an HTTP Session.” Just to be sure, though, am I breaking any Vaadin rules by using a constructor in Application that takes a param?

Cheers,
Bobby