Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //  The contents of this file are subject to the Mozilla Public License
  //  Version 1.1 (the "License"); you may not use this file except in
  //  compliance with the License. You may obtain a copy of the License
  //  at http://www.mozilla.org/MPL/
  //
  //  Software distributed under the License is distributed on an "AS IS"
  //  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  //  the License for the specific language governing rights and
  //  limitations under the License.
 //
 //  The Original Code is RabbitMQ.
 //
 //  The Initial Developer of the Original Code is VMware, Inc.
 //  Copyright (c) 2007-2011 VMware, Inc.  All rights reserved.
 //
 
 
 package com.rabbitmq.client.impl;
 
 
 
 import static java.util.concurrent.TimeUnit.*;

Manages heartbeat sending for a AMQConnection.

Heartbeats are sent in a dedicated thread that is separate from the main loop thread used for the connection.

 
 final class HeartbeatSender {
 
     private final Object monitor = new Object();
 
     private final FrameHandler frameHandler;
 
 
     private ScheduledFuture<?> future;
 
     private boolean shutdown = false;
 
     private volatile long lastActivityTime;
 
     HeartbeatSender(FrameHandler frameHandler) {
         this. = frameHandler;
     }
 
     public void signalActivity() {
         this. = System.nanoTime();
     }

    
Sets the heartbeat in seconds.
 
     public void setHeartbeat(int heartbeatSeconds) {
         synchronized(this.) {
             if(this.) {
                 return;
             }
 
             // cancel any existing heartbeat task
             if(this. != null) {
                 this..cancel(true);
                 this. = null;
             }
 
             if (heartbeatSeconds > 0) {
                 // wake every heartbeatSeconds / 2 to avoid the worst case
                 // where the last activity comes just after the last heartbeat
                 long interval = .toNanos(heartbeatSeconds) / 2;
                 ScheduledExecutorService executor = createExecutorIfNecessary();
                 Runnable task = new HeartbeatRunnable(interval);
                 this. = executor.scheduleAtFixedRate(
                     taskintervalinterval.);
             }
         }
     }
 
         synchronized (this.) {
             if (this. == null) {
                 this. = Executors.newSingleThreadScheduledExecutor();
             }
             return this.;
         }
     }

    
Shutdown the heartbeat process, if any.
 
     public void shutdown() {
         ExecutorService executorToShutdown = null;
        synchronized (this.) {
            if (this. != null) {
                this..cancel(true);
                this. = null;
            }
            if (this. != null) {
                // to be safe, we shouldn't call shutdown holding the
                // monitor.
                executorToShutdown = this.;
                this. = true;
                this. = null;
            }
        }
        if(executorToShutdown != null) {
            executorToShutdown.shutdown();
        }
    }
    private final class HeartbeatRunnable implements Runnable {
        private final long heartbeatNanos;
        private HeartbeatRunnable(long heartbeatNanos) {
            this. = heartbeatNanos;
        }
        public void run() {
            try {
                long now = System.nanoTime();
                if (now > ( + this.)) {
                    .writeFrame(new Frame(., 0));
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }
New to GrepCode? Check out our FAQ X