1 8 9 package mx4j.examples.mbeans.dynamic; 10 11 import javax.management.MBeanAttributeInfo ; 12 import javax.management.MBeanOperationInfo ; 13 import javax.management.MBeanParameterInfo ; 14 15 import mx4j.AbstractDynamicMBean; 16 17 24 public class DynamicService extends AbstractDynamicMBean 25 { 26 31 private boolean running; 32 private int concurrent; 33 34 public void start() 35 { 36 40 synchronized (this) 41 { 42 running = true; 43 } 44 45 Thread thread = new Thread (new Runnable () 46 { 47 public void run() 48 { 49 simulateClientRequests(); 50 } 51 }); 52 thread.start(); 53 } 54 55 public void stop() 56 { 57 synchronized (this) 58 { 59 running = false; 60 } 61 } 62 63 private void simulateClientRequests() 64 { 65 while (isRunning()) 66 { 67 long interval = Math.round(Math.random() * 1000L) + 1; 69 try 70 { 71 Thread.sleep(interval); 72 } 73 catch (InterruptedException ignored) 74 { 75 } 76 77 Thread thread = new Thread (new Runnable () 79 { 80 public void run() 81 { 82 synchronized (DynamicService.this) 84 { 85 ++concurrent; 86 System.out.println("--DynamicService--" + Thread.currentThread() + "-- Incoming client request -- concurrent clients: " + concurrent); 87 } 88 89 long processing = Math.round(Math.random() * 5000L) + 1; 91 try 92 { 93 Thread.sleep(processing); 94 } 95 catch (InterruptedException ignored) 96 { 97 } 98 99 synchronized (DynamicService.this) 101 { 102 --concurrent; 103 } 104 } 105 }); 106 thread.start(); 107 } 108 } 109 110 public synchronized boolean isRunning() 111 { 112 return running; 113 } 114 115 public synchronized int getConcurrentClients() 116 { 117 return concurrent; 118 } 119 120 121 126 protected MBeanAttributeInfo [] createMBeanAttributeInfo() 127 { 128 return new MBeanAttributeInfo [] 129 { 130 new MBeanAttributeInfo ("Running", "boolean", "The running status of the DynamicService", true, false, true), 131 new MBeanAttributeInfo ("ConcurrentClients", "int", "The number of concurrent clients", true, false, false) 132 }; 133 } 134 135 protected MBeanOperationInfo [] createMBeanOperationInfo() 136 { 137 return new MBeanOperationInfo [] 138 { 139 new MBeanOperationInfo ("start", "Starts the DynamicService", new MBeanParameterInfo [0], "void", MBeanOperationInfo.ACTION), 140 new MBeanOperationInfo ("stop", "Stops the DynamicService", new MBeanParameterInfo [0], "void", MBeanOperationInfo.ACTION) 141 }; 142 } 143 } 144 | Popular Tags |