001: /*
002: * Copyright (c) 2003, Intracom S.A. - www.intracom.com
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * This package and its source code is available at www.jboss.org
019: **/
020: package org.jboss.jmx.adaptor.snmp.agent;
021:
022: import java.util.Date;
023:
024: import javax.management.MBeanServer;
025: import javax.management.ObjectName;
026:
027: import org.jboss.logging.Logger;
028:
029: /**
030: * <tt>Heartbeat</tt> auxiliary class implementing agent heartbeat
031: * schedulling and emission setup
032: *
033: * @version $Revision: 23902 $
034: *
035: * @author <a href="mailto:spol@intracom.gr">Spyros Pollatos</a>
036: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
037: **/
038: public class Heartbeat {
039: /** The notification message field */
040: public static final String NOTIFICATION_MSG = "heartbeat report";
041:
042: /** Logger object */
043: private static final Logger log = Logger.getLogger(Heartbeat.class);
044:
045: /** JMX Agent */
046: private MBeanServer agent = null;
047:
048: /** Heart-beat interval in secs */
049: private long interval = 0;
050:
051: /** Timer name */
052: private ObjectName timer = null;
053:
054: /** The id of the scheduled event */
055: private Integer heartbeatSchedule = null;
056:
057: /**
058: * CTOR
059: **/
060: public Heartbeat(MBeanServer agent, ObjectName timer, long interval) {
061: this .agent = agent;
062: this .timer = timer;
063: this .interval = interval;
064: }
065:
066: /**
067: * Setup the production of heart-beat notifications
068: **/
069: public void start() throws Exception {
070: // Get the heartbeat period in mSecs
071: long period = interval * 1000;
072:
073: if (period <= 0) {
074: log.debug("Heartbeat disabled");
075: return;
076: }
077:
078: // Skip if schedule is already set
079: //
080: if (heartbeatSchedule == null) {
081: try {
082: // Organise schedulled emission of heartbeat notification
083: Object userData = null; // No user payload
084: Date startTime = new Date(); // Start immediately
085: Long nbOccurences = new Long(0); // Go on forever
086:
087: // If timer MBean not registered, exception will be thrown
088: heartbeatSchedule = (Integer) agent.invoke(timer,
089: "addNotification", new Object[] {
090: EventTypes.HEARTBEAT, NOTIFICATION_MSG,
091: userData, startTime, new Long(period),
092: nbOccurences }, new String[] {
093: "java.lang.String", "java.lang.String",
094: Object.class.getName(),
095: Date.class.getName(),
096: Long.TYPE.getName(),
097: Long.TYPE.getName() });
098:
099: log.debug("Heartbeat period set to " + period
100: + " msecs");
101: } catch (Exception e) {
102: log.error("while setting heartbeat notification", e);
103: throw e;
104: }
105: }
106: } // start()
107:
108: /**
109: * Disable heartbeat
110: **/
111: public void stop() throws Exception {
112: if (heartbeatSchedule != null) {
113: try {
114: // Have the schedule removed
115: agent.invoke(timer, "removeNotification",
116: new Object[] { heartbeatSchedule },
117: new String[] { heartbeatSchedule.getClass()
118: .getName() });
119: heartbeatSchedule = null;
120: } catch (Exception e) {
121: log.error("while unsetting heartbeat notification", e);
122: throw e;
123: }
124: }
125: } // stop
126:
127: } // class Heartbeat
|