1 16 package org.jmanage.core.alert.source; 17 18 import org.jmanage.core.alert.AlertSource; 19 import org.jmanage.core.alert.AlertInfo; 20 import org.jmanage.core.config.AlertSourceConfig; 21 import org.jmanage.core.management.*; 22 import org.jmanage.core.util.Loggers; 23 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import java.util.List ; 27 import java.util.LinkedList ; 28 import java.util.Set ; 29 import java.io.IOException ; 30 31 35 public class GaugeAlertSource extends AlertSource{ 36 37 private static final Logger logger = 38 Loggers.getLogger(GaugeAlertSource.class); 39 40 private ObjectName monitorObjName = null; 41 private ObjectNotificationListener listener = null; 42 private ObjectNotificationFilterSupport filter = null; 43 44 public GaugeAlertSource(AlertSourceConfig sourceConfig){ 45 super(sourceConfig); 46 } 47 48 protected void registerInternal(){ 49 50 51 monitorObjName = new ObjectName("jmanage.alerts:name=" + alertName + 52 ",id=" + alertId + ",type=GaugeMonitor"); 53 54 55 Set mbeans = connection.queryNames(monitorObjName); 56 if(mbeans != null && mbeans.size() > 0){ 57 58 connection.unregisterMBean(monitorObjName); 59 } 60 61 62 connection.createMBean("javax.management.monitor.GaugeMonitor", 63 monitorObjName, new Object [0], new String [0]); 64 65 List attributes = new LinkedList (); 66 attributes.add(new ObjectAttribute("GranularityPeriod", new Long (5000))); 67 attributes.add(new ObjectAttribute("NotifyHigh", Boolean.TRUE)); 68 attributes.add(new ObjectAttribute("NotifyLow", Boolean.TRUE)); 69 attributes.add(new ObjectAttribute("ObservedAttribute", 70 sourceConfig.getAttributeName())); 71 attributes.add(new ObjectAttribute("ObservedObject", 73 connection.buildObjectName(sourceConfig.getObjectName()))); 74 connection.setAttributes(monitorObjName, attributes); 75 76 81 82 Object [] params = new Object []{sourceConfig.getHighThreshold(), 83 sourceConfig.getLowThreshold()}; 84 String [] signature = new String []{Number .class.getName(), 85 Number .class.getName()}; 86 connection.invoke(monitorObjName, "setThresholds", params, signature); 87 88 connection.invoke(monitorObjName, "start", new Object [0], new String [0]); 89 90 91 listener = new ObjectNotificationListener(){ 92 public void handleNotification(ObjectNotification notification, 93 Object handback) { 94 try { 95 GaugeAlertSource.this.handler.handle( 96 new AlertInfo(notification)); 97 } catch (Exception e) { 98 logger.log(Level.SEVERE, "Error while handling alert", e); 99 } 100 } 101 }; 102 filter = new ObjectNotificationFilterSupport(); 103 filter.enableType("jmx.monitor.gauge.high"); 104 filter.enableType("jmx.monitor.gauge.low"); 105 filter.enableType("jmx.monitor.error.attribute"); 106 filter.enableType("jmx.monitor.error.type"); 107 filter.enableType("jmx.monitor.error.mbean"); 108 filter.enableType("jmx.monitor.error.runtime"); 109 filter.enableType("jmx.monitor.error.threshold"); 110 connection.addNotificationListener(monitorObjName, 111 listener, filter, null); 112 } 113 114 protected void unregisterInternal() { 115 assert connection != null; 116 assert monitorObjName != null; 117 118 try { 119 120 connection.removeNotificationListener(monitorObjName, listener, 121 filter, null); 122 } catch (Exception e) { 123 logger.log(Level.WARNING, 124 "Error while Removing Notification Listener. error: " + 125 e.getMessage()); 126 } 127 128 try { 129 130 connection.unregisterMBean(monitorObjName); 131 } catch (Exception e) { 132 logger.log(Level.WARNING, 133 "Error while unregistering MBean: " + monitorObjName + 134 ". error: " + e.getMessage()); 135 } 136 137 listener = null; 138 filter = null; 139 } 140 } 141 | Popular Tags |