|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License at |
| 4 | +// http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 8 | +// License for the specific language governing rights and limitations |
| 9 | +// under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developers of the Original Code are LShift Ltd, |
| 14 | +// Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd. |
| 15 | +// |
| 16 | +// Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd, |
| 17 | +// Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd |
| 18 | +// are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial |
| 19 | +// Technologies LLC, and Rabbit Technologies Ltd. |
| 20 | +// |
| 21 | +// Portions created by LShift Ltd are Copyright (C) 2007-2010 LShift |
| 22 | +// Ltd. Portions created by Cohesive Financial Technologies LLC are |
| 23 | +// Copyright (C) 2007-2010 Cohesive Financial Technologies |
| 24 | +// LLC. Portions created by Rabbit Technologies Ltd are Copyright |
| 25 | +// (C) 2007-2010 Rabbit Technologies Ltd. |
| 26 | +// |
| 27 | +// All Rights Reserved. |
| 28 | +// |
| 29 | +// Contributor(s): ______________________________________. |
| 30 | +// |
| 31 | + |
| 32 | +package com.rabbitmq.client.impl; |
| 33 | + |
| 34 | +import com.rabbitmq.client.AMQP; |
| 35 | + |
| 36 | +import java.util.concurrent.ScheduledExecutorService; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.ScheduledFuture; |
| 40 | +import java.io.IOException; |
| 41 | + |
| 42 | +/** |
| 43 | + * Manages heartbeats for a {@link AMQConnection}. |
| 44 | + * <p/> |
| 45 | + * Heartbeats are sent in a dedicated thread that is separate |
| 46 | + * from the main loop thread used for the connection. |
| 47 | + */ |
| 48 | +final class Heartbeater { |
| 49 | + |
| 50 | + private final Object monitor = new Object(); |
| 51 | + |
| 52 | + private final FrameHandler frameHandler; |
| 53 | + |
| 54 | + private ScheduledExecutorService executor; |
| 55 | + |
| 56 | + private ScheduledFuture<?> future; |
| 57 | + |
| 58 | + Heartbeater(FrameHandler frameHandler) { |
| 59 | + this.frameHandler = frameHandler; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Sets the heartbeat in seconds. |
| 64 | + */ |
| 65 | + public void setHeartbeat(int heartbeatSeconds) { |
| 66 | + ScheduledFuture<?> previousFuture = null; |
| 67 | + synchronized (this.monitor) { |
| 68 | + previousFuture = this.future; |
| 69 | + this.future = null; |
| 70 | + } |
| 71 | + |
| 72 | + if (previousFuture != null) { |
| 73 | + previousFuture.cancel(true); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + if (heartbeatSeconds > 0) { |
| 78 | + ScheduledExecutorService executor = createExecutorIfNecessary(); |
| 79 | + ScheduledFuture<?> newFuture = executor.scheduleAtFixedRate( |
| 80 | + new HeartbeatRunnable(), heartbeatSeconds, |
| 81 | + heartbeatSeconds, TimeUnit.SECONDS); |
| 82 | + |
| 83 | + synchronized (this.monitor) { |
| 84 | + this.future = newFuture; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + private ScheduledExecutorService createExecutorIfNecessary() { |
| 91 | + synchronized (this.monitor) { |
| 92 | + if (this.executor == null) { |
| 93 | + this.executor = Executors.newSingleThreadScheduledExecutor(); |
| 94 | + } |
| 95 | + return this.executor; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Shutdown the heartbeat process, if any. |
| 101 | + */ |
| 102 | + public void shutdown() { |
| 103 | + ScheduledFuture<?> future; |
| 104 | + ScheduledExecutorService executor; |
| 105 | + |
| 106 | + synchronized (this.monitor) { |
| 107 | + future = this.future; |
| 108 | + executor = this.executor; |
| 109 | + this.future = null; |
| 110 | + this.executor = null; |
| 111 | + } |
| 112 | + |
| 113 | + if (future != null) { |
| 114 | + future.cancel(true); |
| 115 | + } |
| 116 | + |
| 117 | + if (executor != null) { |
| 118 | + executor.shutdown(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private class HeartbeatRunnable implements Runnable { |
| 123 | + |
| 124 | + public void run() { |
| 125 | + try { |
| 126 | + frameHandler.writeFrame(new Frame(AMQP.FRAME_HEARTBEAT, 0)); |
| 127 | + } catch (IOException e) { |
| 128 | + // ignore |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments