About JMX

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);
  }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值