1 17 package org.apache.servicemix.components.jmx; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.component.ComponentContext; 21 import javax.jbi.management.DeploymentException; 22 import javax.jbi.messaging.InOnly; 23 import javax.jbi.messaging.MessageExchange; 24 import javax.jbi.messaging.MessagingException; 25 import javax.jbi.messaging.NormalizedMessage; 26 import javax.management.MBeanServer ; 27 import javax.management.Notification ; 28 import javax.management.NotificationListener ; 29 import javax.management.ObjectName ; 30 import javax.management.monitor.CounterMonitor ; 31 import javax.xml.transform.Source ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.servicemix.MessageExchangeListener; 36 import org.apache.servicemix.components.util.ComponentSupport; 37 import org.apache.servicemix.jbi.jaxp.StringSource; 38 39 44 public class CounterMonitorComponent extends ComponentSupport implements NotificationListener , MessageExchangeListener { 45 46 private static final Log log = LogFactory.getLog(ComponentSupport.class); 47 private String name; 48 private ObjectName ourName; 49 private String observedObjectName; 50 private String attributeName; 51 private long granularityPeriod =5000; 52 private Number threshold; 53 private Number offset; 54 private MBeanServer mbeanServer; 55 private CounterMonitor counterMonitor = new CounterMonitor (); 56 57 63 public void init(ComponentContext cc) throws JBIException { 64 super.init(cc); 65 validate(); 66 if (mbeanServer == null) { 67 mbeanServer = cc.getMBeanServer(); 68 } 69 try { 70 ObjectName observedName = new ObjectName (observedObjectName); 71 if (name == null) { 72 String type = observedName.getKeyProperty("type"); 73 type = type != null ? type : "UNKNOWN"; 74 name = mbeanServer.getDefaultDomain() + ":type=CounterMonitor_" + type; 75 } 76 ourName = new ObjectName (name); 77 counterMonitor.setNotify(true); 78 counterMonitor.addObservedObject(observedName); 79 counterMonitor.setObservedAttribute(attributeName); 80 counterMonitor.setGranularityPeriod(granularityPeriod); 81 counterMonitor.setDifferenceMode(false); 82 counterMonitor.setInitThreshold(threshold); 83 counterMonitor.setOffset(offset); 84 mbeanServer.registerMBean(counterMonitor, ourName); 85 mbeanServer.addNotificationListener(ourName, this, null, new Object ()); 86 } catch (Exception e) { 87 throw new DeploymentException(e); 88 } 89 } 90 91 97 public void start() throws javax.jbi.JBIException { 98 super.start(); 99 counterMonitor.start(); 100 } 101 102 108 public void stop() throws javax.jbi.JBIException { 109 counterMonitor.stop(); 110 super.stop(); 111 } 112 113 119 public void shutDown() throws javax.jbi.JBIException { 120 stop(); 121 if (ourName != null && mbeanServer != null) { 122 try{ 123 mbeanServer.removeNotificationListener(ourName,this); 124 } catch(Exception e) { 125 throw new JBIException(e); 126 } 127 } 128 super.shutDown(); 129 } 130 131 132 135 public void handleNotification(Notification notification,Object arg1) { 136 try { 137 Source source = new StringSource(notification.getMessage()); 138 InOnly exchange = getExchangeFactory().createInOnlyExchange(); 139 NormalizedMessage message = exchange.createMessage(); 140 message.setContent(source); 141 exchange.setInMessage(message); 142 send(exchange); 143 } 144 catch (Exception e) { 145 log.error("Failed to send Notification message to the NMR"); 146 } 147 148 } 149 150 protected void validate() throws JBIException { 151 if (observedObjectName == null) { 152 throw new DeploymentException("observedObjectName is null"); 153 } 154 if (attributeName == null) { 155 throw new DeploymentException("attributeName is null"); 156 } 157 if (threshold == null) { 158 throw new DeploymentException("threshold is null"); 159 } 160 if (offset == null) { 161 throw new DeploymentException("offset is null"); 162 } 163 } 164 165 168 public String getAttributeName() { 169 return attributeName; 170 } 171 172 176 public void setAttributeName(String attributeName) { 177 this.attributeName = attributeName; 178 } 179 180 183 public CounterMonitor getCounterMonitor() { 184 return counterMonitor; 185 } 186 187 190 public void setCounterMonitor(CounterMonitor counterMonitor) { 191 this.counterMonitor = counterMonitor; 192 } 193 194 197 public long getGranularityPeriod() { 198 return granularityPeriod; 199 } 200 201 204 public void setGranularityPeriod(long granularityPeriod) { 205 this.granularityPeriod = granularityPeriod; 206 } 207 208 211 public MBeanServer getMbeanServer() { 212 return mbeanServer; 213 } 214 215 218 public void setMbeanServer(MBeanServer mbeanServer) { 219 this.mbeanServer = mbeanServer; 220 } 221 222 225 public String getName() { 226 return name; 227 } 228 229 232 public void setName(String name) { 233 this.name = name; 234 } 235 236 239 public String getObservedObjectName() { 240 return observedObjectName; 241 } 242 243 246 public void setObservedObjectName(String observedObjectName) { 247 this.observedObjectName = observedObjectName; 248 } 249 250 253 public Number getOffset() { 254 return offset; 255 } 256 257 260 public void setOffset(Number offset) { 261 this.offset = offset; 262 } 263 264 267 public Number getThreshold() { 268 return threshold; 269 } 270 271 274 public void setThreshold(Number threshold) { 275 this.threshold = threshold; 276 } 277 278 public void onMessageExchange(MessageExchange exchange) throws MessagingException { 279 } 281 } 282 | Popular Tags |