Search code examples
javaspringvaadinvaadin4spring

How to use PollListener in vaadin?


I'm trying to use PollListener in vaadin with following code:

@VaadinUI
@PreserveOnRefresh
public class ApplicationUI extends UI {
    @Override
    protected void init(VaadinRequest request) {
        setPollInterval(1000);
        access(new Runnable() {
            @Override
            public void run() {
                System.out.println("TEST POLL: " + counter++); //is only printed a single time
            }
        });
    }
}

The output "TEST POLL 0" is printed a single time when I open my application. But that's it. What might I have missed?


Solution

  • You don't have to do anything, the polling example specifically states that:

    By doing this the browser will poll the server each "timeout" ms and retrieve any possibly pending changes

    So, whatever you did in you application will be updated on the client browser when the next polling occurs. In the example you should see that label being displayed 5 seconds later after the UI has loaded, without any special user interaction.

    However if you need to execute some code with each such request, then you can add a pollingListener

    @Override
    protected void init(VaadinRequest request) {
        setPollInterval(1000);
        addPollListener(new UIEvents.PollListener() {
            @Override
            public void poll(UIEvents.PollEvent event) {
                log.debug("Polling");
            }
        });
    }