我一直在四处寻找并且没有找到使用spring注释生成JMX通知的任何示例。我找到了使用@ManagedAttribute和@ManagedOperation的示例。
由于 - 比尔
答案 0 :(得分:5)
你走了:
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()
触发通知。请务必订阅通知。 :)
答案 1 :(得分:1)
您也可以使用@ManagedNotifications批注将通知详细信息(Notification MetaData)添加到公开的JMX MBean。
在JMXDemo类上应用波纹管注释以及@ManagedResource注释
@ManagedNotifications({ @ManagedNotification(name = "javax.management.Notification", notificationTypes = { "notification type" }, description = "notification description") })
以上详细信息将在“ JConsole通知详细信息”选项下显示通知详细信息。