/hudson-remoting/src/main/java/hudson/remoting/PingThread.java

http://github.com/hudson/hudson · Java · 125 lines · 61 code · 15 blank · 49 comment · 1 complexity · 2fe20d3b549889d6b5e617bead9a297a MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.remoting;
  25. import java.io.IOException;
  26. import java.util.concurrent.ExecutionException;
  27. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  28. import java.util.concurrent.TimeoutException;
  29. import java.util.logging.Logger;
  30. /**
  31. * Periodically perform a ping.
  32. *
  33. * <p>
  34. * Useful when a connection needs to be kept alive by sending data,
  35. * or when the disconnection is not properly detected.
  36. *
  37. * <p>
  38. * {@link #onDead()} method needs to be overrided to define
  39. * what to do when a connection appears to be dead.
  40. *
  41. * @author Kohsuke Kawaguchi
  42. * @since 1.170
  43. */
  44. public abstract class PingThread extends Thread {
  45. private final Channel channel;
  46. /**
  47. * Time out in milliseconds.
  48. * If the response doesn't come back by then, the channel is considered dead.
  49. */
  50. private final long timeout;
  51. /**
  52. * Performs a check every this milliseconds.
  53. */
  54. private final long interval;
  55. public PingThread(Channel channel, long timeout, long interval) {
  56. super("Ping thread for channel "+channel);
  57. this.channel = channel;
  58. this.timeout = timeout;
  59. this.interval = interval;
  60. setDaemon(true);
  61. }
  62. public PingThread(Channel channel, long interval) {
  63. this(channel, 4*60*1000/*4 mins*/, interval);
  64. }
  65. public PingThread(Channel channel) {
  66. this(channel,10*60*1000/*10 mins*/);
  67. }
  68. public void run() {
  69. try {
  70. while(true) {
  71. long nextCheck = System.currentTimeMillis()+interval;
  72. ping();
  73. // wait until the next check
  74. long diff;
  75. while((diff=nextCheck-System.currentTimeMillis())>0)
  76. Thread.sleep(diff);
  77. }
  78. } catch (ChannelClosedException e) {
  79. LOGGER.fine(getName()+" is closed. Terminating");
  80. } catch (IOException e) {
  81. onDead();
  82. } catch (InterruptedException e) {
  83. // use interruption as a way to terminate the ping thread.
  84. LOGGER.fine(getName()+" is interrupted. Terminating");
  85. }
  86. }
  87. private void ping() throws IOException, InterruptedException {
  88. Future<?> f = channel.callAsync(new Ping());
  89. try {
  90. f.get(timeout,MILLISECONDS);
  91. } catch (ExecutionException e) {
  92. if (e.getCause() instanceof RequestAbortedException)
  93. return; // connection has shut down orderly.
  94. onDead();
  95. } catch (TimeoutException e) {
  96. onDead();
  97. }
  98. }
  99. /**
  100. * Called when ping failed.
  101. */
  102. protected abstract void onDead();
  103. private static final class Ping implements Callable<Void, IOException> {
  104. private static final long serialVersionUID = 1L;
  105. public Void call() throws IOException {
  106. return null;
  107. }
  108. }
  109. private static final Logger LOGGER = Logger.getLogger(PingThread.class.getName());
  110. }