/** * Adds a notification listener to the target bean. * * @throws Exception */ protected void addNotificationListener() throws Exception { JMXEndpoint ep = (JMXEndpoint) getEndpoint(); NotificationFilter nf = ep.getNotificationFilter(); ObjectName objectName = ep.getJMXObjectName(); getServerConnection().addNotificationListener(objectName, this, nf, ep.getHandback()); }
/** * Processes the Notification received. The handback will be set as the header "jmx.handback" * while the Notification will be set as the body. * * <p>If the format is set to "xml" then the Notification will be converted to XML first using * {@link NotificationXmlFormatter} * * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, * java.lang.Object) */ public void handleNotification(Notification aNotification, Object aHandback) { JMXEndpoint ep = (JMXEndpoint) getEndpoint(); Exchange exchange = getEndpoint().createExchange(ExchangePattern.InOnly); Message message = exchange.getIn(); message.setHeader("jmx.handback", aHandback); try { if (ep.isXML()) { message.setBody(getFormatter().format(aNotification)); } else { message.setBody(aNotification); } getProcessor().process(exchange); } catch (NotificationFormatException e) { getExceptionHandler().handleException("Failed to marshal notification", e); } catch (Exception e) { getExceptionHandler().handleException("Failed to process notification", e); } }
/** * Initializes the mbean server connection and starts listening for Notification events from the * object. */ @Override protected void doStart() throws Exception { super.doStart(); JMXEndpoint ep = (JMXEndpoint) getEndpoint(); // connect to the mbean server if (ep.isPlatformServer()) { setServerConnection(ManagementFactory.getPlatformMBeanServer()); } else { JMXServiceURL url = new JMXServiceURL(ep.getServerURL()); String[] creds = {ep.getUser(), ep.getPassword()}; Map<String, String[]> map = Collections.singletonMap(JMXConnector.CREDENTIALS, creds); JMXConnector connector = JMXConnectorFactory.connect(url, map); setServerConnection(connector.getMBeanServerConnection()); } // subscribe addNotificationListener(); }
/** Removes the consumer as a listener from the bean. */ protected void removeNotificationListener() throws Exception { JMXEndpoint ep = (JMXEndpoint) getEndpoint(); getServerConnection().removeNotificationListener(ep.getJMXObjectName(), this); }