You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsingPolling.asciidoc 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. ---
  2. title: Using Polling
  3. order: 17
  4. layout: page
  5. ---
  6. [[using-polling]]
  7. = Using Polling
  8. To set up polling for your UI, you only need to set a poll interval
  9. using `UI.setPollInterval(timeout)`. By doing this the browser will poll
  10. the server each "timeout" ms and retrieve any possibly pending changes.
  11. You can test this in practice by creating a small application which
  12. initially creates a small "please wait" UI and then loads the actual UI
  13. in a background thread.
  14. [source,java]
  15. ....
  16. public class PollingUI extends UI {
  17. @WebServlet(value = "/*")
  18. @VaadinServletConfiguration(productionMode = false, ui = Polling7UI.class)
  19. public static class Servlet extends VaadinServlet {
  20. }
  21. @Override
  22. protected void init(VaadinRequest request) {
  23. setContent(new Label("Loading data, please wait..."));
  24. setPollInterval(1000);
  25. new Thread(new Loader()).start();
  26. }
  27. class Loader implements Runnable {
  28. @Override
  29. public void run() {
  30. // Simulate a heavy database operation
  31. try {
  32. Thread.sleep(4500);
  33. } catch (InterruptedException e) {
  34. }
  35. // Wrap UI updates in access to properly deal with locking
  36. access(new Runnable() {
  37. @Override
  38. public void run() {
  39. setContent(new Label("This is the real content"));
  40. // Stop polling once the update is done
  41. setPollInterval(-1);
  42. }
  43. });
  44. }
  45. }
  46. }
  47. ....
  48. For more information regarding locking the session, see [Using server
  49. initiated events]
  50. [[polling-for-multiple-components]]
  51. Polling for multiple components
  52. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  53. If you have the situation that several components need polling at some
  54. point you should use some kind of Manager to handle the polling, for it
  55. can only be set UI-wise (which makes perfectly sense)
  56. A simple `UIPollingManager` which always uses the lowest registered
  57. `intervalTime` could look like this:
  58. [source,java]
  59. ....
  60. import java.util.Collection;
  61. import java.util.HashMap;
  62. import java.util.Map;
  63. import java.util.WeakHashMap;
  64. import com.vaadin.ui.UI;
  65. public class UIPollingManager
  66. {
  67. private Map<UI, Map<Object, Integer>> pollRequests;
  68. public UIPollingManager()
  69. {
  70. pollRequests = new WeakHashMap<>(); // Let's use weak references in case someone forgets to unregister properly
  71. }
  72. /**
  73. * Registers a poll request for the given UI. Sets the pollInterval of this UI to the lowest registered interval.
  74. * @param ui
  75. * @param requestor
  76. * @param pollIntervalInMillis poll interval in milliseconds
  77. */
  78. public void registerPollRequest(UI ui, Object requestor, int pollIntervalInMillis)
  79. {
  80. Map<Object, Integer> uiRequests = pollRequests.get(ui);
  81. if (uiRequests == null)
  82. {
  83. uiRequests = new HashMap<>();
  84. pollRequests.put(ui, uiRequests);
  85. }
  86. uiRequests.put(requestor, pollIntervalInMillis);
  87. setPollInterval(ui);
  88. }
  89. /**
  90. * Removes a poll request for the given UI (if existent). Sets the pollInterval of this UI to the lowest registered interval
  91. * remaining or -1 if no more requests exist for the UI
  92. * @param ui
  93. * @param requestor
  94. */
  95. public void unregisterPollRequest(UI ui, Object requestor)
  96. {
  97. Map<Object, Integer> uiRequests = pollRequests.get(ui);
  98. if (uiRequests != null)
  99. {
  100. uiRequests.remove(requestor);
  101. // Remove the UI from our map if no requests exist anymore
  102. if (uiRequests.size() <= 0) pollRequests.remove(ui);
  103. }
  104. setPollInterval(ui);
  105. }
  106. /**
  107. * Removes all poll requests of the given UI and sets the pollInterval to -1
  108. * @param ui
  109. */
  110. public void unregisterAllPollRequests(UI ui)
  111. {
  112. pollRequests.remove(ui);
  113. ui.setPollInterval(-1);
  114. }
  115. /**
  116. * Sets the pollInterval of the given UI to the lowest registered interval time of this UI
  117. * @param ui
  118. */
  119. private void setPollInterval(UI ui)
  120. {
  121. Map<Object, Integer> uiRequests = pollRequests.get(ui);
  122. if (uiRequests != null)
  123. {
  124. ui.setPollInterval(getLowestNumber(uiRequests.values()));
  125. }
  126. }
  127. /**
  128. * Returns the lowest number of a given Integer-Collection. Returns -1 if no valid Integer is included in the collection.
  129. * @param intervalArray
  130. * @return
  131. */
  132. private Integer getLowestNumber(Collection<Integer> intervalArray)
  133. {
  134. Integer lowestNum = null;
  135. for (Integer i : intervalArray)
  136. {
  137. if (i != null && ( lowestNum == null || i < lowestNum )) lowestNum = i;
  138. }
  139. if (lowestNum == null) return -1;
  140. else
  141. return lowestNum;
  142. }
  143. }
  144. ....
  145. The changed example could then look like this:
  146. [source,java]
  147. ....
  148. public class Polling7UI extends UI {
  149. private UIPollingManager pollingManager; // Instantiate this via Spring or get it via Singleton or whatever
  150. @WebServlet(value = "/*")
  151. @VaadinServletConfiguration(productionMode = false, ui = Polling7UI.class)
  152. public static class Servlet extends VaadinServlet {
  153. }
  154. @Override
  155. protected void init(VaadinRequest request) {
  156. setContent(new Label("Loading data, please wait..."));
  157. Loader loader = new Loader();
  158. pollingManager.registerPollRequest(this, loader, 1000);
  159. new Thread(loader).start();
  160. }
  161. class Loader implements Runnable {
  162. private UI ui;
  163. private UIPollingManager pollingManager;
  164. public Loader( UI ui, UIPollingManager pollingManager )
  165. {
  166. this.ui = ui;
  167. this.pollingManager = pollingManager;
  168. }
  169. @Override
  170. public void run() {
  171. // Simulate a heavy database operation
  172. try {
  173. Thread.sleep(4500);
  174. } catch (InterruptedException e) {
  175. }
  176. final Loader loader = this;
  177. // Wrap UI updates in access to properly deal with locking
  178. access(new Runnable() {
  179. @Override
  180. public void run() {
  181. setContent(new Label("This is the real content"));
  182. // Stop polling once the update is done
  183. pollingManager.unregisterPollRequest(ui, loader);
  184. }
  185. });
  186. }
  187. }
  188. }
  189. ....