Vaadin push and UI.getCurrent returing null

Hi,

AFAICT the pattern with Vaadin push is that you register your component with a worker thread that calls back the component when there is something to update. During the callback the component should apply its changes using a Runnable inside UI.getCurrent().access().

However, UI.getCurrent() returns null if your callback component is a simple Layout for example. In the blog post the widget extends UI and thus doesn’t have this issue.

My solution for now what i’m doing is to register both the component and UI.getCurrent with the worker thread, and in the callback i pass the corresponding UI so that the component can call access() on it .Is this acceptable ?

Thanks
Jorg

Yes, this is a very good pattern.

I created
ticket 12023
for making sure there is a tutorial example that explicitly does it in that way instead of just doing the update directly through the UI.

UI.getCurrent() is a thread-local variable, so it can’t be available in an arbitrary thread unless someone sets it. UI.access() actually does just that (in addition to setting other Vaadin thread-locals and guaranteeing the session is locked), but obviously needs a valid UI instance first. If you pass a Vaadin component to the worker thread that you know is attached to a UI, you can just call component.getUI().access(). Inside the access Runnable, you can then use UI.getCurrent() if you wish.

This is key though. Not all worker threads are attached to a UI (as in they are started from a UI thread).