无法使 Spring JMX NotificationListener 工作

发布于 2024-10-04 09:49:38 字数 3152 浏览 0 评论 0原文

我已经使用 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 code> 没有被执行?代码没有抛出任何异常。

有人让 JMX NotificationListener 与 Spring 一起使用吗?

I have configured a ManagedBean using @ManagedResource annotation using Spring. And also mapped a JMX NotificationListener to this. But I am seeing that the Listener never gets kicked-off/executed.

Here are the related configuration files:

<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>

Here is how the MyMBean class looks like:

@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;
    }
}

And finally, here's how the NotificationListener looks like:

public class MyMBeanNotificationListener implements
        NotificationListener {

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

}

Any idea why the NotificationListener is not getting executed? There isn't any exception thrown by the code.

Has anyone got the JMX NotificationListeners working with Spring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-10-11 09:49:38

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

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

It's not getting executed because you probabily have lazy-load enabled, just set explicitly set lazy-init to false on JMX beans.

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

爱的那么颓废 2024-10-11 09:49:38

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

尝试将:更改

<entry key="myMBean" value-ref="myMBeanNotificationListener" />

为:

<entry key="com.sample:bean=myMBean" value-ref="myMBeanNotificationListener" />

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

<context:mbean-export default-domain="myDomain"/>

Have you seen the notifications appear in jConsole or jVisualVM?

Try changing:

<entry key="myMBean" value-ref="myMBeanNotificationListener" />

to:

<entry key="com.sample:bean=myMBean" value-ref="myMBeanNotificationListener" />

If not for notifications, you could simpify the XML above to:

<context:mbean-export default-domain="myDomain"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文