@Override
protected void addNotificationListener() throws Exception {
JMXEndpoint ep = (JMXEndpoint) getEndpoint();
// create the monitor bean
Monitor bean = null;
if (ep.getMonitorType().equals("counter")) {
CounterMonitor counter = new CounterMonitor();
counter.setInitThreshold(ep.getInitThreshold());
counter.setOffset(ep.getOffset());
counter.setModulus(ep.getModulus());
counter.setDifferenceMode(ep.isDifferenceMode());
counter.setNotify(true);
bean = counter;
} else if (ep.getMonitorType().equals("gauge")) {
GaugeMonitor gm = new GaugeMonitor();
gm.setNotifyHigh(ep.isNotifyHigh());
gm.setNotifyLow(ep.isNotifyLow());
gm.setDifferenceMode(ep.isDifferenceMode());
Object attr = ManagementFactory.getPlatformMBeanServer().getAttribute(ep.getJMXObjectName(), ep.getObservedAttribute());
Double highValue = ep.getThresholdHigh();
Double lowValue = ep.getThresholdLow();
if (attr instanceof Byte) {
gm.setThresholds(highValue.byteValue(), lowValue.byteValue());
} else if (attr instanceof Integer) {
gm.setThresholds(highValue.intValue(), lowValue.intValue());
} else if (attr instanceof Short) {
gm.setThresholds(highValue.shortValue(), lowValue.shortValue());
} else if (attr instanceof Long) {
gm.setThresholds(highValue.longValue(), lowValue.longValue());
} else if (attr instanceof Float) {
gm.setThresholds(highValue.floatValue(), lowValue.floatValue());
} else {
gm.setThresholds(highValue, lowValue);
}
bean = gm;
} else if (ep.getMonitorType().equals("string")) {
StringMonitor sm = new StringMonitor();
sm.setNotifyDiffer(ep.isNotifyDiffer());
sm.setNotifyMatch(ep.isNotifyMatch());
sm.setStringToCompare(ep.getStringToCompare());
bean = sm;
}
bean.addObservedObject(ep.getJMXObjectName());
bean.setObservedAttribute(ep.getObservedAttribute());
bean.setGranularityPeriod(ep.getGranularityPeriod());
// register the bean
mMonitorObjectName = new ObjectName(ep.getObjectDomain(), "name", "camel-jmx-monitor-" + UUID.randomUUID());
ManagementFactory.getPlatformMBeanServer().registerMBean(bean, mMonitorObjectName);
// add ourselves as a listener to it
NotificationFilter nf = ep.getNotificationFilter();
getServerConnection().addNotificationListener(mMonitorObjectName, this, nf, bean);
bean.start();
}