The XMOJO Project
<< Prev Chapter 5.4 Notification Model Example Next >>

Notification Mechanism: A Working Example


To know about a Notification and the JMX Notification model, please refer to the Notification Model section.

 About this example 

In this example, we will see how to send, receive, and filter notification.  Here, we will be using the following resources:
  1. A Dynamic MBean capable of sending notification -- NotifSender.java
  2. A Standard MBean capable of receiving notification -- NotifReceiver.java  and its management interface, NotifReceiverMBean.java
  3. A class NotifFilter capable of filtering notification of type Server.stopped -- NotifFilter.java
  4. A class NotifListener capable of receiving notification -- NotifListener.java
  5. A simple JMX agent for registering the above-mentioned MBeans and adaptors -- RunNotifAgent.java

 Sending Notification 

The Dynamic MBean (NotifSender) has defined with operations startService, stopService.  These operations, startService and stopService when invoked will send Notification of type Server.started and Server.stopped respectively.

 Registering Listeners 

For the above dynamic MBean, two listeners are registered as notification listeners.  One is the Standard MBean (NotifReceiver) and the other is the NotifListener object.  The listeners are added in such a way that the Standard MBean is capable of receiving all notifications, but the NotifListener cannot receive notification of type Server.stopped.

 Receiving Notification 

When the operation startService is invoked on the Dynamic MBean, a notification of type Server.started will be sent to both the registered listeners,  i.e, handleNotification method of NotifReceiver and NotifListener will be called.

When the operation stopService is invoked on the Dynamic MBean, a notification of type Server.stopped will be sent only to the Standard MBean (NotifReceiver) and not to the NotifListener.  (NotifListener cannot receive notification of type Server.stopped).

 Running the Example 

 For Windows OS 

  1. Change your working directory to %XMOJO_HOME%/examples/notification directory.
  2. Execute the build.bat (This batch file compiles the Java files under the src directory and the output class files will be stored in the classes directory).
  3. Execute the run.bat  (This batch file executes the RunNotifAgent class).
The JMX Agent will be started.  RMI Server will be started at port 1099.  HTML Server will be started at port 8030.  For testing the example, refer to the section Testing the Agent given below.

 For Unix OS 

  1. Change your working directory to $XMOJO_HOME/examples/notification directory.
  2. Execute the build.sh (This script file compiles the Java files under the src directory and the output class files will be stored in the classes directory).
  3. Execute the run.sh  (This script file executes the RunNotifAgent class).
The JMX Agent will be started.  RMI Server will be started at port 1099.  HTML Server will be started at port 8030.  For testing the example, refer to the section Testing the Agent given below:

 Testing the Agent 

 Using the RMI Client 

Start the MBeanBrowser tool by executing the mbeanbrowser.bat/mbeanbrowser.sh file under XMOJO_HOME/bin directory.
Connect to the RMI Server.  (In the menu bar, choose Settings >> Client Settings.  Configure the HostName and PortNumber: here, HostName is the machine name where the RMI Server is running.  After configuring the client settings, choose Operations >> Connect.)  This establishes a connection with the RMI Server.

The registered MBeans is listed in the left frame.

Under the domain MyDynMBean, an MBean with the name ability=sender,category=notif will be present.  Under this MBean node, click Operations.  Invoke the stopService operation.  Now, a notification of type server.stopped with the message Server successfully stopped will be sent by the DynamicMBean.  This notification will be broadcasted only to the standard MBean (NotifReceiver) and not to the NotifListener class.  The handleNotification method of NotifReceiver class will be executed resulting in the following print statements (in the JMX agent):

  ***************************************************************************
  **  Notification received !!  Inside handleNotification of NotifReceiver
  **  Notification type : Server.stopped
  **  Notification sequence number : 1
  **  Notification time stamp : 1056470225218
  **  Notification message : Server successfully stopped
  **************************************************************************

Invoke the operation startService.  Now, a notification of type server.started with the message Server successfully started will be sent by the DynamicMBean.  This notification will be broadcasted to both the StandardMBean (NotifReceiver) and the NotifListener class.  The handleNotification method of NotifReceiver and NotifListener class will be executed resulting in the following print statements (in the JMX agent):

  ***************************************************************************
  **  Notification received !!  Inside handleNotification of NotifReceiver
  **  Notification type : Server.started
  **  Notification sequence number : 1
  **  Notification time stamp : 1056470793468
  **  Notification message : Server successfully started
  **************************************************************************
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~~  Notification received !!  Inside handleNotification of NotifListener
  ~~  Notification type : Server.started
  ~~  Notification sequence number : 1
  ~~  Notification time stamp : 1056470793468
  ~~  Notification message : Server successfully started
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 Using the Web Browser 

Open any web browser and type the URL http://localhost:8030.  If you are testing from a remote machine, then enter the URL http://hostName:8030; here, hostName is the machine name where the JMX agent is running.  If the HTML Adaptor is listening at some other port, then replace the 8030 with the appropriate port number in the above URL.

The index page lists the various domains and some useful links for viewing notifications, searching the MBeans, customizing MBean views, etc..  Click the MyDynMBean link under Domains.  One MBean is listed.

Click the link MyDynMBean:ability=sender,category=notif.  Click on the image with the name Operations.  Invoke the stopServer method.  Click the Continue button.  Now, a notification of type server.stopped with the message Server successfully stopped will be sent by the DynamicMBean.  This notification will be broadcasted only to the standard MBean (NotifReceiver) and not to the NotifListener class.  The handleNotification method of NotifReceiver class will be executed resulting in the following print statements (in the JMX agent):

  ***************************************************************************
  **  Notification received !!  Inside handleNotification of NotifReceiver
  **  Notification type : Server.stopped
  **  Notification sequence number : 1
  **  Notification time stamp : 1056470225218
  **  Notification message : Server successfully stopped
  **************************************************************************

Invoke the operation startService.  Now, a notification of type server.started with the message Server successfully started will be sent by the DynamicMBean.  This notification will be broadcasted to both the StandardMBean (NotifReceiver) and the NotifListener class.  The handleNotification method of NotifReceiver and NotifListener class will be executed resulting in the following print statements (in the JMX agent):

  ***************************************************************************
  **  Notification received !!  Inside handleNotification of NotifReceiver
  **  Notification type : Server.started
  **  Notification sequence number : 1
  **  Notification time stamp : 1056470793468
  **  Notification message : Server successfully started
  **************************************************************************
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~~  Notification received !!  Inside handleNotification of NotifListener
  ~~  Notification type : Server.started
  ~~  Notification sequence number : 1
  ~~  Notification time stamp : 1056470793468
  ~~  Notification message : Server successfully started
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
<< Prev Home Next >>
Model MBean Example
Frequently Asked Questions