Problem regarding auto calling method

Hi,
I have a class that extends VerticalLayout. This class has a method. and I want to execute this method automatically in every 10 seconds. Anyone have idea about it?

User Java’s ScheduledExecutorService or just use Timer api.

it gives me this error: Can’t access states without lock of vaadinsession or UI.

That is to be expected - https://vaadin.com/docs/latest/advanced/server-push

Even tho I would rethink the whole idea of creating so many threads on the server and look for a way to move this whole logic to the client side if possible

ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
ses.scheduleAtFixedRate(this::demo, 20, 20, TimeUnit.SECONDS);

public void demo(){
UI.getCurrent().access(() → {
System.out.println(“Task 2 running…”+mqttMessage);
HL.add(new H4(mqttMessage.toString()));
});
}

I tried with it but it is not calling the demo method.

The UI is not available in the thread, you have to get a reference to it and pass it to the thread

UI.getCurrent() is not valid

can you give me an example? So I can understand better way.

https://vaadin.com/docs/latest/advanced/long-running-tasks

This is the screenshot of my code:

I want to display "Hello this function is executed " in an interval of 1 second. This code only displays one time. So there is a problem in this class. could you help me?

The problem is that Runnable is executed every 1 second but UI is not updating.

Did you add push?

you need to have the access and reference of the UI to execute via other thread e.g. “getUI().access(() → …”

Done. I got it now.

Thank you so much for helping.:smiley: