gpt4 book ai didi

java - 无法使 Spring JMX NotificationListener 工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:25:59 26 4
gpt4 key购买 nike

我已经使用 Spring 使用 @ManagedResource 注释配置了一个 ManagedBean。并且还将 JMX NotificationListener 映射到此。 但我发现监听器永远不会启动/执行

相关配置文件如下:

<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 NotificationListener 与 Spring 一起工作吗?

最佳答案

它没有被执行,因为您可能启用了延迟加载,只需在 JMX bean 上将 set lazy-init 显式设置为 false。

示例:http://java.dzone.com/articles/exposing-pojo-jmx-mbean-easily?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+javalobby/frontpage+(Javalobby+/+Java+Zone )

关于java - 无法使 Spring JMX NotificationListener 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com