Search code examples
javaspringannotationsjmx

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


I've been looking around and haven't found any examples of using spring annotations to generate JMX notifications. I have found examples using @ManagedAttribute and @ManagedOperation.

Thanks -Bill


Solution

  • 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 your Spring configuration file you must 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 above bean with JConsole and trigger notifications with the operation trigger(). Be sure to subscribe to the notifications. :)