1 7 package test.compliance.core.notification; 8 9 import javax.management.Notification ; 10 import javax.management.NotificationListener ; 11 import org.jboss.logging.Logger; 12 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 13 import org.jboss.util.threadpool.BasicThreadPool; 14 import org.jboss.util.threadpool.ThreadPool; 15 16 23 public class AsynchNotificationBroadcasterSupport 24 extends JBossNotificationBroadcasterSupport 25 { 26 private static Logger log = Logger.getLogger(AsynchNotifier.class); 28 31 private static ThreadPool defaultPool = new BasicThreadPool("AsynchNotificationBroadcasterSupport"); 32 35 private static long defaultNotificationTimeout; 36 37 40 private long notificationTimeout; 41 private ThreadPool pool; 42 43 public static synchronized void setDefaultThreadPool(ThreadPool tp) 44 { 45 defaultPool = tp; 46 } 47 48 public static long getDefaultNotificationTimeout() 49 { 50 return defaultNotificationTimeout; 51 } 52 53 public static void setDefaultNotificationTimeout(long defaultNotificationTimeout) 54 { 55 AsynchNotificationBroadcasterSupport.defaultNotificationTimeout = defaultNotificationTimeout; 56 } 57 58 60 64 public AsynchNotificationBroadcasterSupport() 65 { 66 this(defaultNotificationTimeout, defaultPool); 67 } 68 69 76 public AsynchNotificationBroadcasterSupport(long notificationTimeout) 77 { 78 this(notificationTimeout, defaultPool); 79 } 80 81 88 public AsynchNotificationBroadcasterSupport(long notificationTimeout, 89 ThreadPool pool) 90 { 91 this.notificationTimeout = notificationTimeout; 92 this.pool = pool; 93 } 94 95 97 public long getNotificationTimeout() 98 { 99 return notificationTimeout; 100 } 101 102 public void setNotificationTimeout(long notificationTimeout) 103 { 104 this.notificationTimeout = notificationTimeout; 105 } 106 107 public ThreadPool getThreadPool() 108 { 109 return pool; 110 } 111 112 public void setThreadPool(ThreadPool pool) 113 { 114 this.pool = pool; 115 } 116 117 119 126 public void handleNotification(NotificationListener listener, 127 Notification notification, 128 Object handback) 129 { 130 AsynchNotifier notifier = new AsynchNotifier(listener, notification, handback); 131 pool.run(notifier, 0, notificationTimeout); 132 } 133 134 139 protected void stopThreadPool(boolean immeadiate) 140 { 141 if(pool != defaultPool) 142 { 143 pool.stop(immeadiate); 144 } 145 } 146 147 149 public class AsynchNotifier 150 implements Runnable 151 { 152 NotificationListener listener; 153 Notification notification; 154 Object handback; 155 156 public AsynchNotifier(NotificationListener listener, 157 Notification notification, 158 Object handback) 159 { 160 this.listener = listener; 161 this.notification = notification; 162 this.handback = handback; 163 } 164 165 public void run() 166 { 167 try 168 { 169 listener.handleNotification(notification, handback); 170 } 171 catch(Throwable throwable) 172 { 173 log.error("Error processing notification=" + notification + 174 " listener=" + listener + 175 " handback=" + handback, 176 throwable); 177 } 178 } 179 } 180 } 181 | Popular Tags |