Search code examples
javajakarta-eevaadincdiwildfly-8

REST-triggered CDI event fail to update Vaadin UI


In my application, a restful web service can fire CDI events (tested and working, events are fired when expected and a simple observer method will log the event as planned).

However, in most case, I would need these events to update the UI of the management console, which is a Vaadin 7.3 application, using Vaadin-CDI.

When the service is called, the event is fired, and here is the resulting error:

Caused by: java.lang.IllegalStateException: CDI listener identified, but there is no active UI available.

Debugger in hand, I have already checked that CDI is properly started. Stuff is injected, @Observes method is properly called etc...

Here is the code of the Vaadin UI:

@CDIUI
public class Console extends UI {

    @Inject
    private PersonDao dao; // Properly injected

    private Layout layout;

    private void addClickedLabel() {
        Label label = new Label("Clicked !!");
        layout.addComponent(label);
    }

    @Override
    protected void init(final VaadinRequest vaadinRequest) {
        layout = new FormLayout();
        Button b = new Button("Click me !!");
        layout.addComponent(b);
        b.addClickListener(clickEvent -> { addClickedLabel(); });
        this.setContent(layout);
    }

    // Method called, but exception raised before !st line is executed.
    private void receiveConnectionEvent(@Observes final ConnectionEvent event) {
        UI.getCurrent().access(() -> {
            String desc = String desc = String.format("Event: %s from %s.", event.getType(), event.getSource());
            Label label = new Label(desc);
            layout.addComponent(label);
        });
    }
}

I've done my RTFM thing, tried my luck with google, any help on how to properly update UIs from CDI events would be really great!


Solution

  • Actually, the manual has the solution... https://vaadin.com/book/-/page/advanced.push.html

    The broadcaster pattern works just fine once you make it CDI-aware.