【发布时间】:2011-09-16 14:12:38
【问题描述】:
我一直在环顾四周,并没有找到任何使用 spring 注释生成 JMX 通知的示例。我找到了使用@ManagedAttribute 和@ManagedOperation 的示例。
谢谢 -比尔
【问题讨论】:
标签: java spring annotations jmx
我一直在环顾四周,并没有找到任何使用 spring 注释生成 JMX 通知的示例。我找到了使用@ManagedAttribute 和@ManagedOperation 的示例。
谢谢 -比尔
【问题讨论】:
标签: java spring annotations jmx
给你:
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);
}
}
}
并且在您的 Spring 配置文件中,您必须使用如下内容:
<?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>
然后您可以使用 JConsole 访问上述 bean 并使用操作 trigger() 触发通知。请务必订阅通知。 :)
【讨论】:
您也可以使用 @ManagedNotifications 注释向公开的 JMX MBean 添加通知详细信息(通知元数据)。
在 Class JMXDemo 上应用下面的注释以及 @ManagedResource 注释
@ManagedNotifications({ @ManagedNotification(name = "javax.management.Notification", notificationTypes = { "notification type" }, description = "notification description") })
以上详细信息将在 JConsole 通知详细信息选项下显示通知详细信息。
【讨论】: