Any examples of using Spring @ManagedNotification annotation to generate JMX messages

I searched and did not find examples of using spring annotations to generate JMX notifications. I found examples using @ManagedAttribute and @ManagedOperation.

Thanks Bill

+4
source share
1 answer

Here you go:

import java.util.concurrent.atomic.AtomicLong; import javax.management.Notification; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.notification.NotificationPublisher; import org.springframework.jmx.export.notification.NotificationPublisherAware; @ManagedResource public class JMXDemo implements NotificationPublisherAware { private final AtomicLong notificationSequence = new AtomicLong(); private NotificationPublisher notificationPublisher; @Override public void setNotificationPublisher( final NotificationPublisher notificationPublisher) { this.notificationPublisher = notificationPublisher; } @ManagedOperation public void trigger() { if (notificationPublisher != null) { final Notification notification = new Notification("type", getClass().getName(), notificationSequence.getAndIncrement(), "The message"); notificationPublisher.sendNotification(notification); } } } 

And in the Spring configuration file, you should use something like this:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <context:mbean-server id="mbeanServer" /> <context:mbean-export server="mbeanServer" /> <bean class="org.springframework.jmx.export.MBeanExporter"> <property name="server" ref="mbeanServer" /> <property name="namingStrategy"> <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy"> <property name="attributeSource"> <bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" /> </property> </bean> </property> </bean> </beans> 

You can then access the bean using the JConsole and trigger notifications using the trigger() operation. Do not forget to subscribe to notifications. :)

+5
source

All Articles