HeartbeatThread.java revision 1183
759N/A/*
759N/A * CDDL HEADER START
759N/A *
759N/A * The contents of this file are subject to the terms of the
759N/A * Common Development and Distribution License, Version 1.0 only
759N/A * (the "License"). You may not use this file except in compliance
759N/A * with the License.
759N/A *
759N/A * You can obtain a copy of the license at
759N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
759N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
759N/A * See the License for the specific language governing permissions
759N/A * and limitations under the License.
759N/A *
759N/A * When distributing Covered Code, include this CDDL HEADER in each
759N/A * file and include the License file at
759N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
759N/A * add the following below this CDDL HEADER, with the fields enclosed
873N/A * by brackets "[]" replaced with your own identifying information:
759N/A * Portions Copyright [yyyy] [name of copyright owner]
759N/A *
759N/A * CDDL HEADER END
759N/A *
759N/A *
759N/A * Portions Copyright 2007 Sun Microsystems, Inc.
759N/A */
759N/A
1182N/Apackage org.opends.server.replication.protocol;
759N/A
759N/Aimport org.opends.server.api.DirectoryThread;
868N/Aimport static org.opends.server.loggers.debug.DebugLogger.debugEnabled;
868N/Aimport static org.opends.server.loggers.debug.DebugLogger.debugVerbose;
868N/Aimport static org.opends.server.loggers.debug.DebugLogger.debugInfo;
759N/A
759N/Aimport java.io.IOException;
759N/A
759N/A/**
759N/A * This thread publishes a heartbeat message on a given protocol session at
1183N/A * regular intervals when there are no other replication messages being
759N/A * published.
759N/A */
759N/Apublic class HeartbeatThread extends DirectoryThread
759N/A{
759N/A
759N/A /**
759N/A * For test purposes only to simulate loss of heartbeats.
759N/A */
759N/A static private boolean heartbeatsDisabled = false;
759N/A
759N/A /**
759N/A * The session on which heartbeats are to be sent.
759N/A */
759N/A private ProtocolSession session;
759N/A
759N/A
759N/A /**
759N/A * The time in milliseconds between heartbeats.
759N/A */
759N/A private long heartbeatInterval;
759N/A
759N/A
759N/A /**
759N/A * Set this to stop the thread.
759N/A */
759N/A private boolean shutdown = false;
759N/A
759N/A
759N/A /**
759N/A * Create a heartbeat thread.
759N/A * @param threadName The name of the heartbeat thread.
759N/A * @param session The session on which heartbeats are to be sent.
759N/A * @param heartbeatInterval The desired interval between heartbeats in
759N/A * milliseconds.
759N/A */
759N/A public HeartbeatThread(String threadName, ProtocolSession session,
759N/A long heartbeatInterval)
759N/A {
759N/A super(threadName);
759N/A this.session = session;
759N/A this.heartbeatInterval = heartbeatInterval;
759N/A }
759N/A
759N/A /**
759N/A * {@inheritDoc}
759N/A */
759N/A @Override
759N/A public void run()
759N/A {
759N/A try
759N/A {
868N/A if (debugEnabled())
868N/A {
868N/A debugInfo("Heartbeat thread is starting, interval is %d",
868N/A heartbeatInterval);
868N/A }
759N/A HeartbeatMessage heartbeatMessage = new HeartbeatMessage();
759N/A
759N/A while (!shutdown)
759N/A {
759N/A long now = System.currentTimeMillis();
868N/A if (debugEnabled())
868N/A {
868N/A debugVerbose("Heartbeat thread awoke at %d, last message was sent " +
868N/A "at %d", now, session.getLastPublishTime());
868N/A }
759N/A
759N/A if (now > session.getLastPublishTime() + heartbeatInterval)
759N/A {
759N/A if (!heartbeatsDisabled)
759N/A {
868N/A if (debugEnabled())
868N/A {
868N/A debugVerbose("Heartbeat sent at %d", now);
868N/A }
759N/A session.publish(heartbeatMessage);
759N/A }
759N/A }
759N/A
759N/A try
759N/A {
759N/A long sleepTime = session.getLastPublishTime() +
868N/A heartbeatInterval - now;
759N/A if (sleepTime <= 0)
759N/A {
759N/A sleepTime = heartbeatInterval;
759N/A }
759N/A
868N/A if (debugEnabled())
868N/A {
868N/A debugVerbose("Heartbeat thread sleeping for %d", sleepTime);
868N/A }
759N/A Thread.sleep(sleepTime);
759N/A }
759N/A catch (InterruptedException e)
759N/A {
759N/A // Keep looping.
759N/A }
759N/A }
759N/A }
759N/A catch (IOException e)
759N/A {
868N/A if (debugEnabled())
868N/A {
868N/A debugInfo("Heartbeat thread could not send a heartbeat.");
868N/A }
759N/A // This will be caught in another thread.
759N/A }
759N/A finally
759N/A {
868N/A if (debugEnabled())
868N/A {
868N/A debugInfo("Heartbeat thread is exiting.");
868N/A }
759N/A }
759N/A }
759N/A
759N/A
759N/A /**
759N/A * Call this method to stop the thread.
759N/A */
759N/A public void shutdown()
759N/A {
759N/A shutdown = true;
759N/A }
759N/A
759N/A
759N/A /**
759N/A * For testing purposes only to simulate loss of heartbeats.
759N/A * @param heartbeatsDisabled Set true to prevent heartbeats from being sent.
759N/A */
759N/A public static void setHeartbeatsDisabled(boolean heartbeatsDisabled)
759N/A {
759N/A HeartbeatThread.heartbeatsDisabled = heartbeatsDisabled;
759N/A }
759N/A}