KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > remote > AbstractHeartBeat


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.remote;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * Base implementation for the HeartBeat interface.
16  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
17  * @version $Revision: 1.2 $
18  */

19 public abstract class AbstractHeartBeat implements HeartBeat, Runnable JavaDoc
20 {
21    private final ConnectionNotificationEmitter emitter;
22    private long pulsePeriod;
23    private int maxRetries;
24    private Thread JavaDoc thread;
25    private volatile boolean stopped;
26
27    /**
28     * Creates a new HeartBeat.
29     * @param emitter The NotificationEmitter that sends connection failures notifications.
30     * @param environment The environment that may contain properties that specify heart beat's behavior
31     * @see #sendConnectionNotificationFailed
32     * @see MX4JRemoteConstants#CONNECTION_HEARTBEAT_PERIOD
33     * @see MX4JRemoteConstants#CONNECTION_HEARTBEAT_RETRIES
34     */

35    protected AbstractHeartBeat(ConnectionNotificationEmitter emitter, Map JavaDoc environment)
36    {
37       this.emitter = emitter;
38       if (environment != null)
39       {
40          try
41          {
42             pulsePeriod = ((Long JavaDoc)environment.get(MX4JRemoteConstants.CONNECTION_HEARTBEAT_PERIOD)).longValue();
43          }
44          catch (Exception JavaDoc ignored)
45          {
46          }
47          try
48          {
49             maxRetries = ((Integer JavaDoc)environment.get(MX4JRemoteConstants.CONNECTION_HEARTBEAT_RETRIES)).intValue();
50          }
51          catch (Exception JavaDoc ignored)
52          {
53          }
54       }
55       if (pulsePeriod <= 0) pulsePeriod = 5000;
56       if (maxRetries <= 0) maxRetries = 3;
57    }
58
59    public long getPulsePeriod()
60    {
61       return pulsePeriod;
62    }
63
64    public int getMaxRetries()
65    {
66       return maxRetries;
67    }
68
69    /**
70     * Subclasses will implement this method using protocol specific connections.
71     * Normally the method {@link javax.management.MBeanServerConnection#getDefaultDomain} is used
72     * to "ping" the server side.
73     */

74    protected abstract void pulse() throws IOException JavaDoc;
75
76    public void start() throws IOException JavaDoc
77    {
78       thread = new Thread JavaDoc(this, "Connection HeartBeat");
79       thread.setDaemon(true);
80       thread.start();
81    }
82
83    public void stop() throws IOException JavaDoc
84    {
85       if (stopped) return;
86       stopped = true;
87       thread.interrupt();
88    }
89
90    public void run()
91    {
92       try
93       {
94          int retries = 0;
95          while (!stopped && !thread.isInterrupted())
96          {
97             try
98             {
99                Thread.sleep(pulsePeriod);
100
101                try
102                {
103                   pulse();
104                   retries = 0;
105                }
106                catch (IOException JavaDoc x)
107                {
108                   if (retries++ == maxRetries)
109                   {
110                      // The connection has died
111
sendConnectionNotificationFailed();
112                      break;
113                   }
114                }
115             }
116             catch (InterruptedException JavaDoc x)
117             {
118                Thread.currentThread().interrupt();
119             }
120          }
121       }
122       finally
123       {
124          stopped = true;
125       }
126    }
127
128    /**
129     * Sends the connection failed notification using the emitter specified in
130     * {@link #AbstractHeartBeat}
131     */

132    protected void sendConnectionNotificationFailed()
133    {
134       emitter.sendConnectionNotificationFailed();
135    }
136 }
137
Popular Tags