无法使Spring JMX NotificationListener工作

pea*_*kit 9 java notifications spring jmx

@ManagedResource使用Spring 使用注释配置了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>
Run Code Online (Sandbox Code Playgroud)

以下是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,这是NotificationListener看起来像:

public class MyMBeanNotificationListener implements
        NotificationListener {

    @Override
    public void handleNotification(Notification notification, Object handback) {
        System.out.println("In Notification Listener" + notification);
    }

}
Run Code Online (Sandbox Code Playgroud)

知道为什么NotificationListener没有被执行?代码没有抛出任何异常.

有没有人让JMX NotificationListener与Spring合作?

Pet*_*ins 0

您是否看到 jConsole 或 jVisualVM 中出现通知?

尝试改变:

<entry key="myMBean" value-ref="myMBeanNotificationListener" />
Run Code Online (Sandbox Code Playgroud)

到:

<entry key="com.sample:bean=myMBean" value-ref="myMBeanNotificationListener" />
Run Code Online (Sandbox Code Playgroud)

如果不是为了通知,您可以将上面的 XML 简化为:

<context:mbean-export default-domain="myDomain"/>
Run Code Online (Sandbox Code Playgroud)