Some benefits of using JMX in your applications
■ Configuration—Using MBeans to make your applications more configurable.
■ Componentization—Using MBeans to break your applications into components,allowing you to alter or replace component implementations.
■ MBeans using MBeans—Combining both previous concepts, one MBean used another MBean to initialize one of its member variables.
**Using MBean notifications**
Emitting notifications is a powerful tool and can be used for communication,alerts, and more. Because notifications can contain important information and valuable data, it is a good idea to keep a record of them.
An MBean wanting to emit Notification objects needs to extend the NotificationBroadcasterSupport class.If the MBean implements the NotificationBroadcaster interface in order to send notifications, then it also defines a method addNotificationListener().Because you have manually created this MBean, you can also invoke its add listener method to add your own listener.
About notification,you can print it or persist it.
To persist notifications,you can either create an MBean to act as a listener on every NotificationBroadcaster in the agent, or you can have each MBean manage its own notification persistence.
About the latter option,you need to use the sendNotification() method, inherited from the NotificationBroadcasterSupport super class.
Example:
import javax.management.*;
public class Polling extends NotificationBroadcasterSupport implements PollingMBean, Runnable{
private boolean stop = true;
private long interval = 1000;
private String info="Notify!";
public Polling(){
}
public void setInterval( long interval ){
long temp = this.interval;
this.interval = interval;
AttributeChangeNotification notif = new AttributeChangeNotification( this, 0, System.currentTimeMillis(), "Attribute Change", "interval", "long", new Long( temp ), new Long( interval ) );
sendNotification( notif );
}
public void start(){
try
{
stop = false;
Thread t = new Thread( this );
t.start();
}
catch( Exception e ){
e.printStackTrace();
}
}
public void stop(){
stop = true;
}
public void run(){
while( !stop ){
try{
Thread.sleep( interval );
System.out.println( "Polling" );
}
catch( Exception e ){
e.printStackTrace();
}
Notification notif = new Notification("com.neu.notify.Polling",this,-1,System.currentTimeMillis(),info);
sendNotification( notif );
}
}
public void sendNotification(Notification notify){
//Save info
super.sendNotification(notify);
}
}