0

我的配置 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"/>

我的代码:Test.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");
    }

}

正在发送通知但未收到通知。任何指针?

4

1 回答 1

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>

希望有帮助。

于 2013-02-13T15:13:57.720 回答