开发者

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开发者_Go百科 JMX notifications. I have found examples using @ManagedAttribute and @ManagedOperation.

Thanks -Bill


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. :)


You can add notification details (Notification MetaData) to the exposed JMX MBean as well using @ManagedNotifications annotation.

Apply bellow annotation on Class JMXDemo along with @ManagedResource annotation

@ManagedNotifications({ @ManagedNotification(name = "javax.management.Notification", notificationTypes = { "notification type" }, description = "notification description") })

Above details will show the notification details under JConsole Notification details option.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜