Sun Java System Application Server Platform Edition 8.2 Developer's Guide

Monitoring Attribute Changes

The demoJMXMonitor() demonstrates how to monitor attribute changes.

Example 16–13 Monitoring Attribute Changes

demoJMXMonitor() throws InstanceNotFoundException, IOException
    final JMXMonitorMgr mgr = getDomainRoot().getJMXMonitorMgr();
    final String attrName = "SampleString";
    final String attrValue = "hello";
    final SampleListener sampleListener = new SampleListener();
    final MBeanServerConnection conn =
        Util.getExtra( mgr ).getConnectionSource()
        sampleListener, null, null );
    final Sample sample = (Sample)getDomainRoot()
        .getContainee( XTypes.SAMPLE );
    final String monitorName = "SampleStringMonitor";
    AMXStringMonitor mon = null;
        try { mgr.remove( monitorName ); }
        catch( Exception e ) {}
        mon = mgr.createStringMonitor( monitorName );
        waitMBeanServerNotification( sampleListener,
            Util.getObjectName( mon ) );
        sample.addAttribute( attrName, attrValue );
        mon.addNotificationListener( sampleListener, null, null);
        mon.setObservedAttribute( attrName );
        mon.setStringToCompare( attrValue );
        mon.setNotifyDiffer( true );
        mon.setNotifyMatch( true );
        mon.addObservedObject( Util.getObjectName( sample ) );
        final StdAttributesAccess attrs = Util.getExtra( sample);
        attrs.setAttribute( new Attribute(attrName, "goodbye") );
        attrs.setAttribute( new Attribute(attrName, attrValue) );
        sample.removeAttribute( attrName );
        final Map notifs = sampleListener.getNotifsReceived();
        waitNumNotifs( notifs,
            AttributeChangeNotification.ATTRIBUTE_CHANGE, 4 );
    catch( Throwable t )
            mon.removeNotificationListener( sampleListener );
            if ( mon != null )
                mgr.remove( mon.getName() );
                waitMBeanServerNotification( sampleListener,
                    Util.getObjectName( mon ) );
            sampleListener );
        catch( ListenerNotFoundException e )

The demoJmx() method demonstrates the implementation of a JMX monitor MBean, that listens for changes in a certain attribute. This is achieved in the following stages:

  1. A instance is obtained using the DomainRoot.getJMXMonitorMgr() method.

  2. A SampleListener JMX notification listener that is provided in the sample package is instantiated.

  3. A connection to the domain’s MBean server is obtained by calling getExistingMBeanServerConnection() on the JMXMonitorMgr instance’s Extra information.

  4. The SampleListener notification listener is added to the MBean server connection, with an MBean server delegate obtained from getMBeanServerDelegateObject(). The notification listener is now in place on the MBean server connection.

  5. An AMX MBean, sample, of the type SAMPLE is obtained by calling the method on an instance of the Sample interface. The Sample interface defines a basic AMX MBean.

  6. An AMXStringMonitor, an AMX-compatible JMX StringMonitorMBean, is instantiated by calling createStringMonitor on the JMXMonitorMgr instance created above. The AMXStringMonitor instance then calls waitMBeanServerNotification(). The waitMBeanServerNotification() method waits for MBean server notifications of the type REGISTRATION_NOTIFICATION from the SampleListener instance that is listening on the MBean server connection.

  7. An attribute of name attrName and value attrValue is added to the AMX MBean sample.

  8. Various methods of the AMXStringMonitor instance are called, to add a listener, and to set the value to be observed, the object to be observed, and so on.

  9. Access to the sample MBean’s attributes is obtained by passing the sample MBean’s Extra information to an instance of The StdAttributesAccess.setAttribute() method is then called to change the values of these attributes.

  10. The AMXStringMonitor then calls the sample notification listener’s getNotifsReceived() method to retrieve the notifications that resulted from the calls to setAttribute() above. The waitNumNotifs() method waits until four ATTRIBUTE_CHANGE notifications have been received before exiting.

  11. The notification listener is then removed and the monitor is closed down.