1 20 package org.jboss.jmx.adaptor.snmp.agent; 21 22 import java.util.Date ; 23 24 import javax.management.MBeanServer ; 25 import javax.management.ObjectName ; 26 27 import org.jboss.logging.Logger; 28 29 38 public class Heartbeat 39 { 40 41 public static final String NOTIFICATION_MSG = "heartbeat report"; 42 43 44 private static final Logger log = Logger.getLogger(Heartbeat.class); 45 46 47 private MBeanServer agent = null; 48 49 50 private long interval = 0; 51 52 53 private ObjectName timer = null; 54 55 56 private Integer heartbeatSchedule = null; 57 58 61 public Heartbeat(MBeanServer agent, ObjectName timer, long interval) 62 { 63 this.agent = agent; 64 this.timer = timer; 65 this.interval = interval; 66 } 67 68 71 public void start() 72 throws Exception 73 { 74 long period = interval * 1000; 76 77 if (period <= 0) { 78 log.debug("Heartbeat disabled"); 79 return; 80 } 81 82 if(heartbeatSchedule == null) { 85 try { 86 Object userData = null; Date startTime = new Date (); Long nbOccurences = new Long (0); 91 heartbeatSchedule = (Integer ) agent.invoke( 93 timer, 94 "addNotification", 95 new Object [] { 96 EventTypes.HEARTBEAT, 97 NOTIFICATION_MSG, 98 userData, 99 startTime, 100 new Long (period), 101 nbOccurences 102 }, 103 new String [] { 104 "java.lang.String", 105 "java.lang.String", 106 Object .class.getName(), 107 Date .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 e) { 115 log.error("while setting heartbeat notification", e); 116 throw e; 117 } 118 } 119 } 121 124 public void stop() 125 throws Exception 126 { 127 if(heartbeatSchedule != null) { 128 try { 129 agent.invoke( 131 timer, 132 "removeNotification", 133 new Object [] { heartbeatSchedule }, 134 new String [] { heartbeatSchedule.getClass().getName() } 135 ); 136 heartbeatSchedule = null; 137 } 138 catch (Exception e) { 139 log.error("while unsetting heartbeat notification", e); 140 throw e; 141 } 142 } 143 } 145 } | Popular Tags |