Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin 8 and async Event handling

Detlef Brendle
4 years ago Apr 26, 2017 8:24am

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

Pontus Boström
4 years ago Apr 26, 2017 8:40am
Herman Bovens
4 years ago Apr 26, 2017 9:30am
Rubén Espinoza
4 years ago Apr 27, 2017 1:08am
Detlef Brendle
4 years ago Apr 27, 2017 6:14am
Pontus Boström
4 years ago Apr 27, 2017 6:39am

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

 

Detlef Brendle
4 years ago Apr 27, 2017 6:44am
Herman Bovens
4 years ago Apr 27, 2017 7:45am
Rubén Espinoza
4 years ago Apr 27, 2017 8:31am