我的配置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));
}
}
和listener ...类NotificationListenerImpl
public class NotificationListenerImpl implements NotificationListener{
public void handleNotification(Notification notification, Object handback) {
// TODO Auto-generated method stub
System.out.println("Notification received");
}
}
正在发送通知但未收到通知。有什么指针吗?
答案 0 :(得分: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的一个例子,它从我所有的注释声明的MBeans中获取通知:
<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>
希望有所帮助。