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

📄 broadcastinguser.java

📁 jmx codeJava源码
💻 JAVA
字号:

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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -