KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > snmp > agent > Heartbeat


1 /*
2  * Copyright (c) 2003, Intracom S.A. - www.intracom.com
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * This package and its source code is available at www.jboss.org
19 **/

20 package org.jboss.jmx.adaptor.snmp.agent;
21
22 import java.util.Date JavaDoc;
23
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 import org.jboss.logging.Logger;
28
29 /**
30  * <tt>Heartbeat</tt> auxiliary class implementing agent heartbeat
31  * schedulling and emission setup
32  *
33  * @version $Revision: 44604 $
34  *
35  * @author <a HREF="mailto:spol@intracom.gr">Spyros Pollatos</a>
36  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
37 **/

38 public class Heartbeat
39 {
40    /** The notification message field */
41    public static final String JavaDoc NOTIFICATION_MSG = "heartbeat report";
42    
43    /** Logger object */
44    private static final Logger log = Logger.getLogger(Heartbeat.class);
45
46    /** JMX Agent */
47    private MBeanServer JavaDoc agent = null;
48
49    /** Heart-beat interval in secs */
50    private long interval = 0;
51    
52    /** Timer name */
53    private ObjectName JavaDoc timer = null;
54    
55    /** The id of the scheduled event */
56    private Integer JavaDoc heartbeatSchedule = null;
57
58    /**
59     * CTOR
60    **/

61    public Heartbeat(MBeanServer JavaDoc agent, ObjectName JavaDoc timer, long interval)
62    {
63       this.agent = agent;
64       this.timer = timer;
65       this.interval = interval;
66    }
67     
68    /**
69     * Setup the production of heart-beat notifications
70    **/

71    public void start()
72       throws Exception JavaDoc
73    {
74       // Get the heartbeat period in mSecs
75
long period = interval * 1000;
76    
77       if (period <= 0) {
78          log.debug("Heartbeat disabled");
79          return;
80       }
81         
82       // Skip if schedule is already set
83
//
84
if(heartbeatSchedule == null) {
85          try {
86             // Organise schedulled emission of heartbeat notification
87
Object JavaDoc userData = null; // No user payload
88
Date JavaDoc startTime = new Date JavaDoc(); // Start immediately
89
Long JavaDoc nbOccurences = new Long JavaDoc(0); // Go on forever
90

91             // If timer MBean not registered, exception will be thrown
92
heartbeatSchedule = (Integer JavaDoc) agent.invoke(
93                timer,
94                "addNotification",
95                new Object JavaDoc[] {
96                   EventTypes.HEARTBEAT,
97                   NOTIFICATION_MSG,
98                   userData,
99                   startTime,
100                   new Long JavaDoc(period),
101                   nbOccurences
102                },
103                new String JavaDoc[] {
104                   "java.lang.String",
105                   "java.lang.String",
106                   Object JavaDoc.class.getName(),
107                   Date JavaDoc.class.getName(),
108                   Long.TYPE.getName(),
109                   Long.TYPE.getName()
110                });
111
112             log.debug("Heartbeat period set to " + period + " msecs");
113          }
114          catch (Exception JavaDoc e) {
115             log.error("while setting heartbeat notification", e);
116             throw e;
117          }
118       }
119    } // start()
120

121    /**
122     * Disable heartbeat
123    **/

124    public void stop()
125       throws Exception JavaDoc
126    {
127       if(heartbeatSchedule != null) {
128          try {
129             // Have the schedule removed
130
agent.invoke(
131                timer,
132                "removeNotification",
133                new Object JavaDoc[] { heartbeatSchedule },
134                new String JavaDoc[] { heartbeatSchedule.getClass().getName() }
135             );
136             heartbeatSchedule = null;
137          }
138          catch (Exception JavaDoc e) {
139             log.error("while unsetting heartbeat notification", e);
140             throw e;
141          }
142       }
143    } // stop
144

145 } // class Heartbeat
146
Popular Tags