1 8 9 package mx4j.examples.mbeans.legacy; 10 11 import java.lang.reflect.Field ; 12 import java.lang.reflect.Method ; 13 import javax.management.ListenerNotFoundException ; 14 import javax.management.MBeanAttributeInfo ; 15 import javax.management.MBeanNotificationInfo ; 16 import javax.management.MBeanOperationInfo ; 17 import javax.management.MBeanParameterInfo ; 18 import javax.management.Notification ; 19 import javax.management.NotificationBroadcaster ; 20 import javax.management.NotificationBroadcasterSupport ; 21 import javax.management.NotificationFilter ; 22 import javax.management.NotificationListener ; 23 24 import mx4j.AbstractDynamicMBean; 25 26 39 public class DynamicLegacyService extends AbstractDynamicMBean implements NotificationBroadcaster 40 { 41 45 private LegacyService service; 46 private Thread statusThread; 47 48 public DynamicLegacyService(LegacyService service) 49 { 50 this.service = service; 51 52 statusThread = new Thread (new Runnable () 53 { 54 public void run() 55 { 56 monitorStatus(); 57 } 58 }); 59 } 60 61 66 public void start() 67 { 68 statusThread.start(); 70 71 service.execute(); 73 } 74 75 78 public boolean isRunning() 79 { 80 try 82 { 83 Class cls = service.getClass(); 84 Method method = cls.getDeclaredMethod("isRunning", new Class [0]); 85 method.setAccessible(true); 86 Boolean result = (Boolean )method.invoke(service, new Object [0]); 87 return result.booleanValue(); 88 } 89 catch (Exception ignored) 90 { 91 ignored.printStackTrace(); 92 return false; 93 } 94 } 95 96 100 public int getThreadCount() 101 { 102 try 105 { 106 Class cls = service.getClass(); 107 Field field = cls.getDeclaredField("group"); 108 field.setAccessible(true); 109 ThreadGroup group = (ThreadGroup )field.get(service); 110 return group.activeCount(); 111 } 112 catch (Exception ignored) 113 { 114 ignored.printStackTrace(); 115 return 0; 116 } 117 } 118 119 124 private void monitorStatus() 125 { 126 boolean wasRunning = false; 127 while (true) 128 { 129 boolean isRunning = isRunning(); 130 if (wasRunning ^ isRunning) 131 { 132 Notification notification = new Notification ("legacy.status.running." + isRunning, this, 0, "Legacy Service Status: " + isRunning); 133 broadcaster.sendNotification(notification); 134 wasRunning = isRunning; 135 } 136 else 137 { 138 if (isRunning) System.out.println("Threads: " + getThreadCount()); 139 } 140 141 try 143 { 144 Thread.sleep(50); 145 } 146 catch (InterruptedException ignored) 147 { 148 } 149 } 150 } 151 152 156 private NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport (); 157 158 protected MBeanAttributeInfo [] createMBeanAttributeInfo() 159 { 160 return new MBeanAttributeInfo [] 161 { 162 new MBeanAttributeInfo ("Running", "boolean", "The running status of the Legacy Service", true, false, true), 163 new MBeanAttributeInfo ("ThreadCount", "int", "The number of running threads", true, false, false) 164 }; 165 } 166 167 protected MBeanOperationInfo [] createMBeanOperationInfo() 168 { 169 return new MBeanOperationInfo [] 170 { 171 new MBeanOperationInfo ("start", "Start the Legacy Service", new MBeanParameterInfo [0], "void", MBeanOperationInfo.ACTION) 172 }; 173 } 174 175 protected MBeanNotificationInfo [] createMBeanNotificationInfo() 176 { 177 return getNotificationInfo(); 178 } 179 180 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 181 { 182 broadcaster.addNotificationListener(listener, filter, handback); 183 } 184 185 public MBeanNotificationInfo [] getNotificationInfo() 186 { 187 return new MBeanNotificationInfo [] 188 { 189 new MBeanNotificationInfo 190 (new String []{"legacy.status.running.true", "legacy.status.running.false"}, 191 Notification .class.getName(), 192 "Notifications on the status of the Legacy Service") 193 }; 194 } 195 196 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 197 { 198 broadcaster.removeNotificationListener(listener); 199 } 200 } 201 | Popular Tags |