【发布时间】:2010-11-25 16:44:44
【问题描述】:
我已经使用 Spring 使用 @ManagedResource 注释配置了一个 ManagedBean。并且还将 JMX NotificationListener 映射到此。 但我发现 Listener 永远不会被启动/执行。
相关配置文件如下:
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<bean id="myMBeanServer"
class="org.springframework.jmx.support.MBeanServerFactoryBean">
<!-- indicate to first look for a server -->
<property name="locateExistingServerIfPossible" value="true" />
</bean>
<!-- MBean auto exporter -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
lazy-init="false">
<property name="server" ref="myMBeanServer" />
<property name="assembler" ref="assembler" />
<property name="namingStrategy" ref="namingStrategy" />
<property name="notificationListenerMappings">
<map>
<entry key="myMBean"
value-ref="myMBeanNotificationListener" />
</map>
</property>
</bean>
<!-- The assembler -->
<bean id="assembler"
class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource" ref="attributeSourceStrategy" />
</bean>
<!-- The naming strategy -->
<bean id="namingStrategy"
class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
<property name="attributeSource" ref="attributeSourceStrategy" />
</bean>
<!-- The attributeSource strategy -->
<bean id="attributeSourceStrategy"
class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
<!-- MyMBean -->
<bean id="myMBean"
class="com.sample.MyMBean" />
<!-- MBean Notification Listener -->
<bean id="myMBeanNotificationListener"
class="com.sample.MyMBeanNotificationListener" />
</beans>
MyMBean 类如下所示:
@ManagedResource(description = "My Mbean", objectName = "com.sample:bean=myMBean")
public class MyMBean {
private boolean isAvailable = true;
@ManagedAttribute(description = "isAvailable", defaultValue = "true")
public void setAvailable(boolean flag) {
this.isAvailable = flag;
}
}
最后,NotificationListener 的外观如下:
public class MyMBeanNotificationListener implements
NotificationListener {
@Override
public void handleNotification(Notification notification, Object handback) {
System.out.println("In Notification Listener" + notification);
}
}
知道为什么NotificationListener 没有被执行吗?代码没有抛出任何异常。
有人让 JMX NotificationListeners 与 Spring 一起工作吗?
【问题讨论】:
-
如果
MyMBean不发布通知,你会期待什么? -
axtavt,请检查我之前的问题stackoverflow.com/questions/4260398/… 有人确认属性更改会广播 JMX 通知。对于上面的听众,我希望能捕捉到这一点。您确定我们需要发布者并且属性更改不会自动发送通知吗?谢谢!
-
你上一个问题的答案中链接的文章明确说需要手动广播
AttributeChangeNotifications。
标签: java spring notifications jmx