Java Reference
In-Depth Information
C
After registering the MBean with the MBean server, the
HelloAgent
class adds
itself to the
HelloWorld
MBean as a listener. To do this, it passes itself as the
NotificationListener
parameter to the MBean's
addNotificationListener()
method (inherited from its super class,
NotificationBroadcasterSupport
).
As mentioned earlier, in order to receive notifications, an object must implement
the
NotificationListener
interface. The interface declares a single method,
handleNotification()
, which is a callback method invoked by the sender to
deliver notifications to the listener. For this implementation, the
HelloAgent
class
just prints out some of the members of the incoming notification.
D
Getting results
To test the notification example, you need to compile the HelloAgent.java and
HelloWorld.java files and execute the resulting
HelloAgent
class. (Look back at
section 2.5 for a reminder.) Once the agent is running, open your web browser to
http://localhost:9092 to see the Agent View of your
HelloAgent
class. To perform
the test and receive a notification, follow these steps:
Navigate to the MBean View of the
HelloWorld
MBean by clicking on
the corresponding object name in the list. Look back at section 2.5.2 for
a refresher.
1
The
HelloWorld
MBean sends a notification when its greeting is changed,
so enter a new value and click Apply. For example, enter
I have changed
my greeting
.
2
Look at the output window of your
HelloAgent
. You should see the following:
3
Receiving notification...
jmxbook.ch2.helloWorld.test
I have changed my greeting
The output contains your printed message, “Receiving notification…”, the noti-
fication type, and the message contained in the notification.
2.7 Summary
This chapter gave you some hands-on experience with much of the
JMX
frame-
work. In this chapter you developed a manageable resource, and created and ran
a simple
JMX
agent. We discussed how to register an MBean, ensure that it has a
unique name, and create an MBean server.
In addition, you worked with the
HTML
adapter that comes with the Sun ref-
erence implementation. By constructing the
HelloWorld
example, you should
now understand that MBean development is simple from the
JMX
point of view.