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.

Heartbeat.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.communication;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.http.client.Request;
  19. import com.google.gwt.http.client.RequestBuilder;
  20. import com.google.gwt.http.client.RequestCallback;
  21. import com.google.gwt.http.client.RequestException;
  22. import com.google.gwt.http.client.Response;
  23. import com.google.gwt.user.client.Timer;
  24. import com.vaadin.client.ApplicationConnection;
  25. import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent;
  26. import com.vaadin.shared.ApplicationConstants;
  27. import com.vaadin.shared.ui.ui.UIConstants;
  28. import com.vaadin.shared.util.SharedUtil;
  29. /**
  30. * Handles sending of heartbeats to the server and reacting to the response
  31. *
  32. * @since 7.2
  33. * @author Vaadin Ltd
  34. */
  35. public class Heartbeat {
  36. private Timer timer = new Timer() {
  37. @Override
  38. public void run() {
  39. send();
  40. }
  41. };
  42. private ApplicationConnection connection;
  43. private String uri;
  44. private int interval = -1;
  45. private static Logger getLogger() {
  46. return Logger.getLogger(Heartbeat.class.getName());
  47. }
  48. /**
  49. * Initializes the heartbeat for the given application connection
  50. *
  51. * @param connection
  52. * the connection
  53. */
  54. public void init(ApplicationConnection applicationConnection) {
  55. connection = applicationConnection;
  56. setInterval(connection.getConfiguration().getHeartbeatInterval());
  57. uri = SharedUtil.addGetParameters(
  58. connection.translateVaadinUri(
  59. ApplicationConstants.APP_PROTOCOL_PREFIX
  60. + ApplicationConstants.HEARTBEAT_PATH + '/'),
  61. UIConstants.UI_ID_PARAMETER + "="
  62. + connection.getConfiguration().getUIId());
  63. connection.addHandler(
  64. ApplicationConnection.ApplicationStoppedEvent.TYPE,
  65. new ApplicationConnection.ApplicationStoppedHandler() {
  66. @Override
  67. public void onApplicationStopped(
  68. ApplicationStoppedEvent event) {
  69. setInterval(-1);
  70. }
  71. });
  72. }
  73. /**
  74. * Sends a heartbeat to the server
  75. */
  76. public void send() {
  77. timer.cancel();
  78. final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);
  79. final RequestCallback callback = new RequestCallback() {
  80. @Override
  81. public void onResponseReceived(Request request, Response response) {
  82. int status = response.getStatusCode();
  83. if (status == Response.SC_OK) {
  84. connection.getConnectionStateHandler().heartbeatOk();
  85. } else {
  86. // Handler should stop the application if heartbeat should
  87. // no longer be sent
  88. connection.getConnectionStateHandler()
  89. .heartbeatInvalidStatusCode(request, response);
  90. }
  91. schedule();
  92. }
  93. @Override
  94. public void onError(Request request, Throwable exception) {
  95. // Handler should stop the application if heartbeat should no
  96. // longer be sent
  97. connection.getConnectionStateHandler()
  98. .heartbeatException(request, exception);
  99. schedule();
  100. }
  101. };
  102. rb.setCallback(callback);
  103. try {
  104. getLogger().fine("Sending heartbeat request...");
  105. rb.send();
  106. } catch (RequestException re) {
  107. callback.onError(null, re);
  108. }
  109. }
  110. /**
  111. * @return the interval at which heartbeat requests are sent
  112. */
  113. public int getInterval() {
  114. return interval;
  115. }
  116. /**
  117. * Updates the schedule of the heartbeat to match the set interval. A
  118. * negative interval disables the heartbeat.
  119. */
  120. public void schedule() {
  121. if (interval > 0) {
  122. getLogger()
  123. .fine("Scheduling heartbeat in " + interval + " seconds");
  124. timer.schedule(interval * 1000);
  125. } else {
  126. getLogger().fine("Disabling heartbeat");
  127. timer.cancel();
  128. }
  129. }
  130. /**
  131. * @return the application connection
  132. */
  133. @Deprecated
  134. protected ApplicationConnection getConnection() {
  135. return connection;
  136. }
  137. /**
  138. * Changes the heartbeatInterval in runtime and applies it.
  139. *
  140. * @param heartbeatInterval
  141. * new interval in seconds.
  142. */
  143. public void setInterval(int heartbeatInterval) {
  144. getLogger().info(
  145. "Setting hearbeat interval to " + heartbeatInterval + "sec.");
  146. interval = heartbeatInterval;
  147. schedule();
  148. }
  149. }