Java Dynamic Management Kit 5.1 Tutorial

15.2 Passive Discovery

In passive discovery, the entity seeking knowledge about agents listens for the activation or deactivation of their discovery responders. When discovery responders are started or stopped, they send out a proprietary message that contains all discovery response information. The DiscoveryMonitor object waits to receive any of these messages from the multicast group.

A discovery monitor is often associated with a discovery client. By relying on the information from both, you can keep an up-to-date list of all agents in a given multicast group.

Figure 15–3 Passive Discovery of Discovery Responders

Diagram showing passive discovery of discovery responders

Therefore, configuring and starting the discovery responder is an important step to the overall discovery strategy of your applications.

15.2.1 Discovery Responder

The agents that are configured to be discovered must have an active DiscoveryResponder registered in their MBean server. The responder plays a role in both active and passive discovery:

Both types of messages are proprietary and their contents are not exposed to the user. These messages contain information about the MBean server, its delegate's information and a list of communicator MBeans, unless not requested by the discovery client.

In our example we create the discovery responder in the MBean server and then activate it. Then, we create different connector servers that will discover the agent passively, due to its active discovery responder.

Example 15–3 Creating a Discovery Responder

public class Responder {
    public static void main(String[] args) {
	      try {
	          MBeanServer myMBeanServer = 

	          echo("\nCreate and register a DiscoveryResponder MBean.");
	          ObjectName dc = 
               new ObjectName("DiscoveryExample:name=DiscoveryResponder");
	          myMBeanServer.invoke(dc, "start", null, null);
	          // Create an HtmlAdaptorServer on the default port.
          // Create JMX Remote API connector servers
	    	    JMXServiceURL url;
	          JMXConnectorServer server;
	          ObjectName on;
	          // rmi
	          url = new JMXServiceURL("rmi", null, 0);
	          server = 
	          url = server.getAddress();
     	    on = new ObjectName("jmx-remote:protocol=rmi");
	          myMBeanServer.registerMBean(server, on);
          // Create RMI/IIOP connector server

          // Create JMXMP connector server

          // stop/start the responder to send a notification
	          myMBeanServer.invoke(dc, "stop", null, null);
	          myMBeanServer.invoke(dc, "start", null, null);

          // Create wrapped legacy RMI and HTTP connector servers 
	          // Repeat for the other current and legacy connector protocols

	          // stop/start the responder to allow a Monitor to find them
	          myMBeanServer.invoke(dc, "stop", null, null);
	          myMBeanServer.invoke(dc, "start", null, null);

	          echo("All servers have been registered.");

	          echo("\n>>> Press return to exit.");
	      } catch (Exception e) {

The discovery responder has attributes for exposing a multicast group and a multicast port. These attributes define a multicast socket that the responder will use to receive discovery requests. It will also send activation and deactivation messages to this multicast group. When sending automatic responses to discovery requests, the time-to-live is provided by the discovery client. The responder's time-to-live attribute is only used when sending activation and deactivation messages.

We use the default settings of the discovery responder that are the multicast group on port 9000 with time-to-live of 1. In order to modify these values, you need to set the corresponding attributes before starting the discovery responder MBean. You can also specify them in the class constructor. If the responder is active, you will need to stop it before trying to set any of these attributes. In that way, it will send a deactivation message using the old values and then an activation message with the new values.

15.2.2 Discovery Monitor

The discovery monitor is a notification broadcaster: when it receives an activation or deactivation message from a discovery responder, it sends a discovery responder notification to its listeners. Once its parameters are configured and the monitor is activated, the discovery is completely passive. You can add or remove listeners at any time.

The DiscoveryMonitor MBean has multicast group and multicast port attributes that determine the multicast socket where it will receive responder messages. Like the other components of the discovery service, the default multicast group is and the default port is 9000. You can specify other values for the group and port either in the constructor or through attribute setters when the monitor is off-line.

Example 15–4 shows a discovery monitor being started, and then a listener being added.

Example 15–4 Instantiating and Starting a Discovery Monitor

public class Monitor {
    public static void main(String[] args) throws Exception {

       echo("Create a DiscoveryMonitor.");
	      DiscoveryMonitor dm = new DiscoveryMonitor();

	      echo("Add a listener to receive monitor notifications.");
	      dm.addNotificationListener(new MyListener(), null, null);
	      echo("Waiting for new server notifications ...");

	      echo("\nType any key to stop the monitor.");;

	      echo("\nThe monitor has been stopped.");


The discovery monitor must be activated with the start operation before it will receive responder messages and send notifications. If it is being used as an MBean, it will be stopped automatically if it is unregistered from its MBean server. If it is not used as an MBean, as is the case here, you should invoke its stop method before your application exits.

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 =;
		              if (!(o instanceof ConnectorAddress)) {
		              ConnectorAddress ca = (ConnectorAddress)o;
		              if (ca.getConnectorType().equals("SUN RMI")) {
			               url = new JMXServiceURL("jdmk-rmi",
		              // Repeat for jdmk-http and jdmk-https connectors

                  } else {

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

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