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) {
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.
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() 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.
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