Search code examples
javaeventsrefreshpollingvaadin7

VAADIN 7: What is the simplest way to refresh a Vaadin View in 5 minute intervals?


I'm trying to implement an automatic refresh on a List of components in a Vaadin view. The list gets its contents from a database. I can refresh the list with a button, that is already implemented.

However, I would like to know what is the simplest way to make it so, that this refresh event, that I already have, would automatically refresh in 5 minute (300 000 milliseconds) intervals?

Here's what I tried and it keeps refreshing even after I exit the view so it doesn't really work. I'd like the refresh to happen in 5 minute intervals only while that certain view is being shown.

UI myUI = UI.getCurrent();  

            myUI.setPollInterval(300000);
            myUI.addPollListener(event -> {
                refreshList();
            });

How would I make it so that the refresh doesn't happen after navigating to other views? And is there some simpler way to do this in Vaadin? Thank you


Solution

  • You could do so that you call

    myUI.setPollInterval(300000);
    

    when the view is activated, and you disable it by calling

    myUI.setPollInterval(-1);
    

    when another view is activated. If you use Vaadin Navigator, then you could use ViewChangeListener for handling the polling.