⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listenerclient.java

📁 jmx codeJava源码
💻 JAVA
字号:

package book.jmx.examples;

import javax.management.*;
import java.util.List;

public class ListenerClient {

  private NotificationListener listener = null;
  private NotificationFilter filter     = null;
  private MBeanServer server            = null;
   
  public void run() {

    // Find an agent from this JVM. Null argument will
    // return a list of all MBeanServer instances.
    List list = MBeanServerFactory.findMBeanServer(null);
    server    = (MBeanServer)list.iterator().next();
         
    // create the listener and filter instances
    listener  = new UserListener(server);
    filter    = new UserFilter();

    // Register three different instances of Broadcasting
    // User MBean to the agent. The single UserListener
    // instance is registered to each MBean. MBean
    // ObjectName is used as a hand-back object.
    try {
      ObjectName john = new ObjectName("user:name=John");
      ObjectName mike = new ObjectName("user:name=Mike");
      ObjectName xena = new ObjectName("user:name=Xena");
      Attribute jName = new Attribute("Name", "John");
      Attribute mName = new Attribute("Name", "Mike");
      Attribute xName = new Attribute("Name", "Xena");
    
      server.registerMBean(new DynamicUser(), john);
      server.registerMBean(new DynamicUser(), mike);
      server.registerMBean(new DynamicUser(), xena);

      server.addNotificationListener(
                john, listener, filter, john);
      server.addNotificationListener(
                mike, listener, filter, mike);
      server.addNotificationListener(
                xena, listener, filter, xena);

      server.setAttribute(john, jName);
      server.setAttribute(mike, mName);
      server.setAttribute(xena, xName);
                
      // Invoke remove on each MBean instance. This
      // will broadcast a notification from the MBean.
      server.invoke(john, "remove", null, null);
      server.invoke(mike, "remove", null, null);
      server.invoke(xena, "remove", null, null);
    }
    catch (JMException e) {
      e.printStackTrace();
    }
  }


  //
  // Notification listener implementation.
  //
  class UserListener implements NotificationListener {
      
    MBeanServer server = null;
      
    UserListener(MBeanServer server) {
      this.server = server;
    }
      
    public void handleNotification(Notification notif,
                                   Object handback) {
      
      String type = notif.getType();
      
      if (type.equals("example.user.remove")) {                                
        try {
          System.out.println(notif.getMessage());
          
          server.unregisterMBean((ObjectName)handback);
          System.out.println(handback + " unregistered.");
        }
        catch (JMException e) {
          e.printStackTrace();
        }
      }
      
      // process attribute change notifications
      else if (type.equals(
          AttributeChangeNotification.ATTRIBUTE_CHANGE)) {
        
        AttributeChangeNotification notification =
          (AttributeChangeNotification)notif;
          
        System.out.println(notification.getMessage()); 
        System.out.println(
          "  New value=" + notification.getNewValue());
      }
    }      
  }

  //
  // Notification filter implementation.
  //
  class UserFilter implements NotificationFilter {
               
    public boolean isNotificationEnabled(Notification n) {
      return 
        (n.getType().equals
            ("example.user.remove") ||
         n.getType().equals
            (AttributeChangeNotification.ATTRIBUTE_CHANGE)
        )
           ? true
           : false;
    }
  }


  //
  // Main method for the client. This will instantiate
  // an agent in the JVM.
  //
  public static void main(String[] args) {
    
    MBeanServer server = 
        MBeanServerFactory.createMBeanServer();
     
    new ListenerClient().run();
  }
   
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -