I’ve a use-case for the polling functionality in Vaadin. This functionality is basically just an interval configured for the client-side. Every time this interval kicks in, the client asks the server for changes and updates its UI accordingly. I need to use this functionality since I’ve a background task which gathers data for a combobox. However, this combobox isn’t crucial for the app itself, so I decided to let it display “disabled” until the data is collected. However, this works fine but I need to configure polling to automatically update the combobox (or better the UI containing the combobox).
To achieve this I’ve created a polling-manager according to the best-practice tutorial in the [Vaadin-Wiki]
[1]
.
#!java
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import com.vaadin.ui.UI;
public class UIPollingManager
{
private Map<UI, Map<Object, Integer>> pollRequests;
public UIPollingManager()
{
pollRequests = new WeakHashMap<>(); // Let's use weak references in case someone forgets to unregister properly
}
/**
* Registers a poll request for the given UI. Sets the pollInterval of this UI to the lowest registered interval.
* @param ui
* @param requestor
* @param pollIntervalInMillis poll interval in milliseconds
*/
public void registerPollRequest(UI ui, Object requestor, int pollIntervalInMillis)
{
Map<Object, Integer> uiRequests = pollRequests.get(ui);
if (uiRequests == null)
{
uiRequests = new HashMap<>();
pollRequests.put(ui, uiRequests);
}
uiRequests.put(requestor, pollIntervalInMillis);
setPollInterval(ui);
}
/**
* Removes a poll request for the given UI (if existent). Sets the pollInterval of this UI to the lowest registered interval
* remaining or -1 if no more requests exist for the UI
* @param ui
* @param requestor
*/
public void unregisterPollRequest(UI ui, Object requestor)
{
Map<Object, Integer> uiRequests = pollRequests.get(ui);
if (uiRequests != null)
{
uiRequests.remove(requestor);
// Remove the UI from our map if no requests exist anymore
if (uiRequests.size() <= 0) pollRequests.remove(ui);
}
setPollInterval(ui);
}
/**
* Removes all poll requests of the given UI and sets the pollInterval to -1
* @param ui
*/
public void unregisterAllPollRequests(UI ui)
{
pollRequests.remove(ui);
ui.setPollInterval(-1);
}
/**
* Sets the pollInterval of the given UI to the lowest registered interval time of this UI
* @param ui
*/
private void setPollInterval(UI ui)
{
Map<Object, Integer> uiRequests = pollRequests.get(ui);
if (uiRequests != null)
{
ui.setPollInterval(getLowestNumber(uiRequests.values()));
}
}
/**
* Returns the lowest number of a given Integer-Collection. Returns -1 if no valid Integer is included in the collection.
* @param intervalArray
* @return
*/
private Integer getLowestNumber(Collection<Integer> intervalArray)
{
Integer lowestNum = null;
for (Integer i : intervalArray)
{
if (i != null && ( lowestNum == null || i < lowestNum )) lowestNum = i;
}
if (lowestNum == null) return -1;
else
return lowestNum;
}
}
The manager there is basically a collection of UI-components with their needed polling-intervals in milliseconds mapped to them. These components are again in a map associated to their correspondenting UIs. However, as far as I know the only entity concerned about polling is basically the UI itself. Also the code only associates polling intervals to the UIs leaving the component entities (like Buttons, Comboboxes etc.) kind of untouched. It updates the whole UI (so each component contained in the UI) in the given frequency anyway.
Why does the manager still keeps accurately track of components? Wouldn’t it be suitable to just have a collection of intervals mapped to UIs?
Basically instead of a map like this:
private Map<UI, Map<Object, Integer>> pollRequests;
A map like this:
// UI to interval (as Integer) mapping
private Map<UI, Integer> pollRequests;