0

my config 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"/>

my code: 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();
    }

}

class 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));
    }
}

and the listener...class NotificationListenerImpl

public class NotificationListenerImpl implements NotificationListener{

    public void handleNotification(Notification notification, Object handback) {

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

}

Notifications are being sent but not received. Any pointers?

Vikram
  • 4,162
  • 8
  • 43
  • 65

1 Answers1

1

Not sure if you resolved this issue yet, but I'll see if I can help. I have been playing with spring/JMX recently and am still new, but hopefully I can share some insight.

I don't think you need to declare the listener as an MBean to export, only the beans that will be publishing notifications. Secondly, I believe that the key in the notificationListenerMappings is intended to be the ObjectName of the MBean, not a reference to the listener's bean itself. In other words..

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

You can also use wild cards for the listener mapping keys. Here is an example of my own MBeanExporter, which picks up notifications from all of my annotation-declared 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>

Hope that helps.

Mac
  • 1,143
  • 6
  • 21
  • 45