【问题标题】:Spring Framework 3.0 JMX Notification not being received未收到 Spring Framework 3.0 JMX 通知
【发布时间】:2013-01-18 21:17:26
【问题描述】:

我的配置 XML:appconfig.xml

<beans xmlns="...">

   <context:mbean-server/>
   <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="beans">
            <map>
                <entry key="bean:name=notificationSender" value-ref="notificationSenderImpl"></entry>
                <entry key="bean:name=notificationListener" value-ref="notificationListenerImpl"></entry>
            </map>
        </property>
    <property name="notificationListenerMappings">
                    <map>
                <entry key="notificationListenerImpl" value-ref="notificationListenerImpl"></entry>
            </map>              
        </property>

        <property name="server" ref="mbeanServer"/>    
    </bean>
    <bean id="notificationSender" class="com....NotificationSenderImpl"/>
    <bean id="notificationListener" class="com....NotificationListenerImpl"/>

我的代码: 测试.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:appconfig.xml")
public class Test {
    @Autowired
    private ConfigurableApplicationContext context;

    @Test
    public void testFlow() {
        NotificationSender sender = (NotificationSender) context.getBean("notificationSender");     
                sender.send();
    }

    @After
    public void tearDown(){
        context.close();
    }

}

类 NotificationSenderImpl.java

public class NotificationSenderImpl implements NotificationPublisherAware{

       private NotificationPublisher notificationPublisher; 

    public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
        // TODO Auto-generated method stub
        this.notificationPublisher = notificationPublisher;     
    }

    public void send(){
        notificationPublisher.sendNotification(new Notification("simple", this, 0L));
    }
}

和监听器...类 NotificationListenerImpl

public class NotificationListenerImpl implements NotificationListener{

    public void handleNotification(Notification notification, Object handback) {

        // TODO Auto-generated method stub
        System.out.println("Notification received");
    }

}

通知正在发送但未收到。有什么指点吗?

【问题讨论】:

    标签: spring spring-jmx


    【解决方案1】:

    不确定您是否已解决此问题,但我会看看能否提供帮助。我最近一直在玩 spring/JMX 并且还是新手,但希望我能分享一些见解。

    我认为您不需要将侦听器声明为要导出的 MBean,只需将要发布通知的 bean 声明为即可。其次,我认为 notificationListenerMappings 中的键是 MBean 的 ObjectName,而不是对侦听器 bean 本身的引用。换句话说..

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
        <property name="beans">
            <map>
                <entry key="bean:name=notificationSender" value-ref="notificationSenderImpl"></entry>
            </map>
        </property>
        <property name="notificationListenerMappings">
            <map>
                <entry key="bean:name=notificationSender" value-ref="notificationListenerImpl"></entry>
            </map>              
        </property>
        <property name="server" ref="mbeanServer"/>    
    </bean>
    

    您还可以为侦听器映射键使用通配符。这是我自己的 MBeanExporter 的示例,它从我所有的注释声明的 MBean 中获取通知:

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
        .
        .
        .
        <property name="notificationListenerMappings">
            <map>
                <entry key="*">
                    <bean class="com.poc.jmx.domain.NotificationBroadcastListener" />
                </entry>
            </map>
        </property>
    </bean>
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 2014-05-21
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多