Java Dynamic Management Kit 5.1 Tutorial

15.2.3 Discovery Responder Notifications

When it receives a responder's activation or deactivation message, the discovery monitor sends notification objects of the DiscoveryResponderNotification class. This notification contains the new state of the discovery responder (ONLINE or OFFLINE) and a DiscoveryResponse object with information from the agent where the responder is located.

The listener could use this information to update a list of agents in the network. In our example, the listener is the agent application itself, and the handler method only prints out the information in the notification.


Example 15–5 Discovery Responder Notification Handler

private static class MyListener implements NotificationListener {
	   public void handleNotification(Notification notif, Object handback) {

	      try {
       		DiscoveryResponderNotification dn 
                               = (DiscoveryResponderNotification)notif;
            DiscoveryResponse dr = (DiscoveryResponse)dn.getEventInfo()  ;

		         JMXServiceURL url = null;

		         // legacy servers
		         Collection c = dr.getObjectList().values();
		         for (Iterator iter=c.iterator(); iter.hasNext();) {
		              Object o = iter.next();
		    
		              if (!(o instanceof ConnectorAddress)) {
			               continue;
		              }
		    
		              ConnectorAddress ca = (ConnectorAddress)o;
		              if (ca.getConnectorType().equals("SUN RMI")) {
			               url = new JMXServiceURL("jdmk-rmi",
                             ((RmiConnectorAddress)ca).getHost(),
                             ((RmiConnectorAddress)ca).getPort());
		              // Repeat for jdmk-http and jdmk-https connectors
		              [...]

                  } else {
			 			       continue;
		              }

                  echo("\nFound a legacy server which is registered as
                       a legacy MBean: "
                       +url.getProtocol()+" "+url.getHost()+" "
                       +url.getPort());
		              echo("Connecting to that server.");
                  JMXConnector jc = JMXConnectorFactory.connect(url);
		      		   jc.close();
		         }

		         // JMX Remote API servers
		         JMXServiceURL[] urls = dr.getServerAddresses();
		
		         echo("");
		         for (int ii=0; ii<urls.length; ii++) {
		              echo("\nFound a server which is registered as a 
                      JMXConnectorServerMBean: "
			                +urls[ii]);
		              echo("Connecting to that server.");
		              JMXConnector jc = JMXConnectorFactory.connect(urls[ii]);
		              echo("Its default domain is 
                       "+jc.getMBeanServerConnection().getDefaultDomain());
		      	      jc.close();
		         }
	        } catch (Exception e) {
		         echo("Got unexpected exception: "+e);
		         e.printStackTrace();
	        }
      }
    }