Notification listener not working properly for JMX

Viewed 34

I have 3 main classes. I run Main1->Main2->Main3 in different command prompt windows. I expect that after running Main3, it will sysout "Received notification!" in Main2 window. But nothing happens.... Why does the listener not triggered?

Main1: registers the mbean

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server"); 
JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, null); 
connector.connect;
MBeanServerConnection server = connector.getMBeanServerConnection();
ObjectName objNm = new ObjectName("Qwerty:type=server1");
server = createMBean(SampleCnt.class.getName(), objNm);

Main2: listens to mbean

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server"); 
JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, null); 
connector.connect;
MBeanServerConnection server = connector.getMBeanServerConnection();
ObjectName objNm = new ObjectName("Qwerty:type=server1");
if (server.isRegistered(objNm)) {
   CntListener listener = new CntListener();
   server.addNotificationListener(objNm, listener, null,null);
}

Main3: sets value to mbean

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server"); 
JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, null); 
connector.connect;
MBeanServerConnection server = connector.getMBeanServerConnection();
ObjectName objNm = new ObjectName("Qwerty:type=server1");
if (server.isRegistered(objNm)) {
   server.setAttribute(objNm, new Attribute("cnt", 1));
}

other codes:

public interface SampleCntMBean {
   public int getCnt();
   public void setCnt(int cnt);
}
public class extends NotificationBroadcasterSupport implements SampleCntMBean {
   volatile private int cnt = 0;
   public int getCnt() {
      return cnt;
   }
   public void setCnt(int cnt) {
      this.cnt = cnt;
   }
}
public class CntListener implements NotificationListener {
   public void handleNotification(Notification notification, Object handback) {  
      System.out.println("Received notification!");  
   } 
}
0 Answers
Related