JEE6 CDI Events with Vaadin

Hi,

today I figured out a problem with the use of CDI Events in Vaadin. I have the fallowing situation: A Window which adds another Window as modal dialog with a form in it. When a user submits that form and it is valid then a delegate-method on the containing Window should be invoked.

To do this with as loosley coupling as possible I used CDI Events. The dialog fires the event and the containing window has a method that observes that particular event. So far all is ok. The event is fired and the observer-method is called.

The problem is I wanted to do some modifications on the containing window in the observer-method, but these are never shown in the window or better say the UIDL sent back to the client never contains the modifications done in that method. And there are no exceptions thrown, all seems to work well…

Here the code I used for testing:


@SessionScoped
public class TestApplication extends Application {

    @Inject Testwindow window;

    @Override
    public void init() {
        setMainWindow(window);
    }
}

public class Testevent {}

public class Testwindow extends Window{

    @Inject TestDialog dialog;

    @PostConstruct
    void init(){

        addComponent(new Button("show dialog", new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                addWindow(dialog);
            }
        }));
    }

    void observerMethod(@Observes Testevent event){
        showNotification("Event triggered");
        System.out.println("Event triggered");
    }
}

public class TestDialog extends Window{

    @Inject
    private javax.enterprise.event.Event<Testevent> eventt;

    @PostConstruct
    void init(){
        addComponent(new Button("Fire", new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                eventt.fire(new Testevent());
                close();
            }
        }));

    }
}

With this code the message
“Event triggered”
is shown on the console of the server but the Notification never appears in the browser.

Do you have any idea why this happens?

Thanks,
Patrick

While I don’t really know CDI Events that well, I believe they are not necessarily called synchronously, nor in the same thread as the calling code. In the transactional case, the events are called at the transaction completion phase, and otherwise are triggered “immediately” but potentially in a different thread (someone can correct me if I’m wrong).

Therefore, your observer method does not properly synchronize on the Vaadin Application instance, and might be completed “too late” so that the the changes to render have already been generated and maybe also sent to the browser. On the server side, the notification is created, but the browser does not ask for additional queued updates.

If that is the case, you could try the
Refresher
or
ICEPush
add-ons to make the browser fetch these updates, and add a
synchronized(getApplication()) { … }
block in the observer method.