Getting the UI in case of callback into a Vaadin application - IllegalState

I am writing a Vaadin Flow application that spins off a discrete-event simulation. The simulation framework runs the simulation model in a separate thread. The simulation allows me to schedule events that do a callback at certain moments in the future.

I would like to use this mechanism to progress a Vaadin ProgressBar in the View that coordinates running the simulation. This will allow the user to see progress as the simulation runs.

When the callback is executed, I am getting an IllegalStateException - “Cannot access state in VaadinSession or UI without locking the session.”, see the stacktrace below.

When trying UI.getCurrent().getSession().lock();, I get a NPE, since UI.getCurrent() returns null.

Could anyone tell me what my options for getting the correct UI object…? Thanks.

Caused by: java.lang.IllegalStateException: Cannot access state in VaadinSession or UI without locking the session.
    at com.vaadin.flow.server.VaadinSession.checkHasLock(VaadinSession.java:515)
    at com.vaadin.flow.server.VaadinSession.checkHasLock(VaadinSession.java:529)
    at com.vaadin.flow.internal.StateTree.checkHasLock(StateTree.java:408)
    at com.vaadin.flow.internal.StateTree.markAsDirty(StateTree.java:279)
    at com.vaadin.flow.internal.StateNode.markAsDirty(StateNode.java:531)
    at com.vaadin.flow.internal.nodefeature.NodeMap.setChanged(NodeMap.java:383)
    at com.vaadin.flow.internal.nodefeature.NodeMap.put(NodeMap.java:195)
    at com.vaadin.flow.internal.nodefeature.ElementPropertyMap.putWithDeferredChangeEvent(ElementPropertyMap.java:171)
    at com.vaadin.flow.internal.nodefeature.ElementPropertyMap.put(ElementPropertyMap.java:143)
    at com.vaadin.flow.internal.nodefeature.AbstractPropertyMap.setProperty(AbstractPropertyMap.java:60)
    at com.vaadin.flow.internal.nodefeature.ElementPropertyMap.setProperty(ElementPropertyMap.java:92)
    at com.vaadin.flow.dom.impl.BasicElementStateProvider.setProperty(BasicElementStateProvider.java:267)
    at com.vaadin.flow.dom.Element.setRawProperty(Element.java:784)
    at com.vaadin.flow.dom.Element.setProperty(Element.java:694)
    at com.vaadin.flow.component.progressbar.GeneratedVaadinProgressBar.setValue(GeneratedVaadinProgressBar.java:164)

Here’s one example: https://vaadin.com/docs/v14/flow/advanced/tutorial-push-access.html

Yes, that seems to be just what I need. Many thanks for your prompt response.