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.

CleaningUpResourcesInAUI.asciidoc 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ---
  2. title: Cleaning Up Resources In A UI
  3. order: 11
  4. layout: page
  5. ---
  6. [[cleaning-up-resources-in-a-ui]]
  7. = Cleaning up resources in a UI
  8. Vaadin UIs that are open on the client side send a regular heartbeat
  9. to the server to indicate they are still alive, even though there is no
  10. ongoing user interaction. When the server does not receive a valid
  11. heartbeat for a given UI, it will eventually remove that UI from the
  12. session.
  13. By default, heartbeats are sent every five minutes, and the server
  14. closes an UI after three missed heartbeats. The heartbeat interval can
  15. be customized by providing an init parameter named `heartbeatInterval`
  16. in your web.xml, setting its value to the desired interval in seconds:
  17. [source,xml]
  18. ....
  19. <web-app>
  20. <context-param>
  21. <param-name>heartbeatInterval</param-name>
  22. <param-value>120</param-value>
  23. </context-param>
  24. <!-- Or per-servlet: -->
  25. <servlet>
  26. <!-- ... -->
  27. <init-param>
  28. <param-name>heartbeatInterval</param-name>
  29. <param-value>120</param-value>
  30. </init-param>
  31. </servlet>
  32. </web-app>
  33. ....
  34. To do custom cleanup in your UIs, `DetachListener`s can be registered:
  35. [source,java]
  36. ....
  37. public class MyUI extends UI {
  38. @Override
  39. protected void init(VaadinRequest request) {
  40. addDetachListener(new DetachListener() {
  41. @Override
  42. public void detach(DetachEvent event) {
  43. releaseSomeResources();
  44. }
  45. });
  46. }
  47. private void releaseSomeResources() {
  48. // ...
  49. }
  50. ....
  51. Of course, your UI could also implement `DetachListener` itself:
  52. [source,java]
  53. ....
  54. public class MyUI extends UI implements DetachListener {
  55. protected void init(VaadinRequest request) {
  56. addDetachListener(this);
  57. }
  58. @Override
  59. public void detach(DetachEvent event) {
  60. // do cleanup
  61. }
  62. }
  63. ....
  64. If you'd like to share a listener instance between multiple UIs, the
  65. current UI can be queried:
  66. [source,java]
  67. ....
  68. public void detach(DetachEvent event) {
  69. // do cleanup
  70. event.getConnector().getUI();
  71. // or equivalent:
  72. UI.getCurrent();
  73. }
  74. ....
  75. Because heartbeat requests are just like any other request from the
  76. servlet container's viewpoint, each heartbeat extends the lifetime of
  77. the underlying HttpSession. This means that as long as there is an open
  78. UI, the session never expires even though there is no user interaction.
  79. This is desirable in some situations, but in others it may be not.
  80. You can control this behavior by setting an init parameter named
  81. `closeIdleSessions` to `true`. When it is set to true (the default is
  82. `false`), the session will be closed if no UI is active. Before the
  83. session is closed, the detach methods will be called, and cleanup is
  84. performed.
  85. [source,xml]
  86. ....
  87. <context-param> <!-- or init-param -->
  88. <param-name>closeIdleSessions</param-name>
  89. <param-value>true</param-value>
  90. </context-param>
  91. ....