Vaadin 8 and async Event handling

Hi,
I have a Vaadin App that is built together with spring boot 1.5.2
Within the App I start an async job which then sends events on the Application Event Bus
The View consumes these Events.
I am not sure what would be the proper way in consuming these async messages.

What I did was:

@EventBusListenerMethod
public void onEvent(org.vaadin.spring.events.Event<DeploymentEvent> event) {
   if (getUI() != null) {
     getUI().access(() -> {
     //update view components here.
     }
   }

Is this the correct way of dealing with the events ?

Thanks,
Detlef

Hi,

That is pretty much the correct pattern. One thing to note is that getUI() is not thread safe either. You should pass along the correct UI instance so that you can call access(…) on it directly. getUI() might also return null in case the component is detached. It might be better to use UI.getCurrent(), but that method should not be called from background threads.

Best regards,
Pontus

Probably the view is unregistered from the event bus in detach(), so it won’t return null.
That being said, you can simply inject the UI into your spring view.

UI.getCurrent () does not work for me. getUI () if it works fine.

@Override
public MytUI getUI() {
   return (MyUI) super.getUI();
}

UI.getCurrent() isnt working for me either.
This is why I use getUI() instead.

UI.getCurrent() should work, but only if it is called from the request (UI) thread. You then need to pass the UI instance to the background thread that updates the UI. You cannot (or should not) use the pattern UI.getCurrent().access(…).

The problems with getUI() used this way is a) only attached components will be updated b) getUI() is not thread safe, even with a null check there might be changes to the component three afterwards that makes getUI().access(…) throw an NPE. I guess these problems are typically pretty insignificant, but still it can be good to keep in mind.

-Pontus

But how do I then deal with multiple users ?
The background thread should notify all open UI’s.
Passing the UI to the background thread only updates this UI, and all other UI’s are not affected.

I thought this is what the Application Event Bus is for. Updating all UI’s

Regards,
Detlef

Just inject the UI into the View and use that reference to call ui.access().

Because as he mentions, Pontus Boström. Now if it is working in many ways,
Thanks a lot
.

MyClass(MyUI ui) {}  ?