1 40 41 package com.sun.jmx.examples.scandir; 42 43 import com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState; 44 import java.io.IOException ; 45 import java.lang.management.ManagementFactory ; 46 import java.util.concurrent.BlockingQueue ; 47 import java.util.concurrent.LinkedBlockingQueue ; 48 import java.util.concurrent.TimeUnit ; 49 import java.util.logging.Logger ; 50 import javax.management.JMException ; 51 import javax.management.Notification ; 52 import javax.management.NotificationEmitter ; 53 import javax.management.NotificationListener ; 54 55 76 public class ScanDirAgent { 77 78 81 private static final Logger LOG = 82 Logger.getLogger(ScanDirAgent.class.getName()); 83 84 private volatile ScanManagerMXBean proxy = null; 87 88 private final BlockingQueue <Notification > queue; 91 92 private final NotificationListener listener; 95 96 102 public ScanDirAgent() { 103 queue = new LinkedBlockingQueue <Notification >(); 105 106 listener = new NotificationListener () { 108 public void handleNotification(Notification notification, 109 Object handback) { 110 try { 111 LOG.finer("Queuing received notification "+notification); 115 queue.put(notification); 116 } catch (InterruptedException ex) { 117 } 119 } 120 }; 121 } 122 123 129 public void init() throws IOException , JMException { 130 131 proxy = ScanManager.register(); 135 136 ((NotificationEmitter )proxy).addNotificationListener(listener,null,null); 140 } 141 142 147 public void cleanup() throws IOException , JMException { 148 try { 149 ((NotificationEmitter )proxy). 150 removeNotificationListener(listener,null,null); 151 } finally { 152 ManagementFactory.getPlatformMBeanServer(). 153 unregisterMBean(ScanManager.SCAN_MANAGER_NAME); 154 } 155 } 156 157 179 public void waitForClose() throws IOException , JMException { 180 181 while(proxy.getState() != ScanState.CLOSED ) { 183 try { 184 queue.poll(30,TimeUnit.SECONDS); 194 } catch (InterruptedException ex) { 195 } 197 } 198 } 199 200 209 public static void main(String [] args) 210 throws IOException , JMException { 211 System.out.println("Initializing ScanManager..."); 212 final ScanDirAgent agent = new ScanDirAgent(); 213 agent.init(); 214 try { 215 System.out.println("Waiting for ScanManager to close..."); 216 agent.waitForClose(); 217 } finally { 218 System.out.println("Cleaning up..."); 219 agent.cleanup(); 220 } 221 } 222 } 223 | Popular Tags |