broadcastinguser.java

来自「jmx codeJava源码」· Java 代码 · 共 93 行

JAVA
93
字号

package book.jmx.examples;

import javax.management.*;

public class BroadcastingUser extends User implements
      BroadcastingUserMBean, NotificationBroadcaster  {

         
   // broadcaster support class
   private NotificationBroadcasterSupport broadcaster = 
               new NotificationBroadcasterSupport();
   
   // sequence number for notifications
   private long notificationSequence = 0;
   
   // override the 'Name' management attribute
   public void setName(String name) {
       String oldValue = super.getName();
       String newValue = name;
       String attrType = String.class.getName();
       String attrName = "Name";
       
       super.setName(name);
       
       broadcaster.sendNotification(
         new AttributeChangeNotification(
           this,                            // source
           ++notificationSequence,          // seq. number
           System.currentTimeMillis(),      // time stamp
           "User's name has been changed.", // message
           attrName, attrType,
           oldValue, newValue
         )
       );
   }
   
   // management operations
   public void remove() {
        
      broadcaster.sendNotification(
         new Notification(
            "example.user.remove",          // type
            this,                           // source
            ++notificationSequence,         // seq. number
            "User " +getName()+ " removed." // message
         )
      );
   }
         
         
   // notification broadcaster implementation      
   
   public void addNotificationListener(
            NotificationListener listener, 
            NotificationFilter filter,
            Object handback) {
         
      broadcaster.addNotificationListener(
               listener, filter, handback);   
   }
                                    
   public void removeNotificationListener(
            NotificationListener listener)
            throws ListenerNotFoundException {
      
      broadcaster.removeNotificationListener(listener);         
   }
                                
   public MBeanNotificationInfo[] getNotificationInfo() {
      return new MBeanNotificationInfo[] {
          
        new MBeanNotificationInfo(
          new String[] 
            { "example.user.remove" },  // notif. types
          Notification.class.getName(), // notif. class
          "User Notifications."         // description
        ),
        
        // attribute change notification type
        new MBeanNotificationInfo(
          new String[] {
            AttributeChangeNotification.ATTRIBUTE_CHANGE
          },
          AttributeChangeNotification.class.getName(),
          "User attribute change notification."
        )
      };
   }

}

⌨️ 快捷键说明

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