1 22 package org.jboss.mx.notification; 23 24 import javax.management.Notification ; 25 import javax.management.NotificationListener ; 26 27 import org.jboss.logging.Logger; 28 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 29 import org.jboss.util.threadpool.BasicThreadPool; 30 import org.jboss.util.threadpool.ThreadPool; 31 32 39 public class AsynchNotificationBroadcasterSupport 40 extends JBossNotificationBroadcasterSupport 41 { 42 private static Logger log = Logger.getLogger(AsynchNotifier.class); 44 45 private static ThreadPool defaultPool = new BasicThreadPool("AsynchNotificationBroadcasterSupport"); 46 47 private static long defaultNotificationTimeout; 48 49 50 private long notificationTimeout; 51 private ThreadPool pool; 52 53 public static synchronized void setDefaultThreadPool(ThreadPool tp) 54 { 55 defaultPool = tp; 56 } 57 58 public static long getDefaultNotificationTimeout() 59 { 60 return defaultNotificationTimeout; 61 } 62 public static void setDefaultNotificationTimeout(long defaultNotificationTimeout) 63 { 64 AsynchNotificationBroadcasterSupport.defaultNotificationTimeout = defaultNotificationTimeout; 65 } 66 67 69 73 public AsynchNotificationBroadcasterSupport() 74 { 75 this(defaultNotificationTimeout, defaultPool); 76 } 77 83 public AsynchNotificationBroadcasterSupport(long notificationTimeout) 84 { 85 this(notificationTimeout, defaultPool); 86 } 87 93 public AsynchNotificationBroadcasterSupport(long notificationTimeout, 94 ThreadPool pool) 95 { 96 this.notificationTimeout = notificationTimeout; 97 this.pool = pool; 98 } 99 100 102 public long getNotificationTimeout() 103 { 104 return notificationTimeout; 105 } 106 public void setNotificationTimeout(long notificationTimeout) 107 { 108 this.notificationTimeout = notificationTimeout; 109 } 110 111 public ThreadPool getThreadPool() 112 { 113 return pool; 114 } 115 public void setThreadPool(ThreadPool pool) 116 { 117 this.pool = pool; 118 } 119 120 122 129 public void handleNotification(NotificationListener listener, 130 Notification notification, 131 Object handback) 132 { 133 AsynchNotifier notifier = new AsynchNotifier(listener, notification, handback); 134 pool.run(notifier, 0, notificationTimeout); 135 } 136 137 141 protected void stopThreadPool(boolean immeadiate) 142 { 143 if( pool != defaultPool ) 144 { 145 pool.stop(immeadiate); 146 } 147 } 148 149 151 public class AsynchNotifier 152 implements Runnable 153 { 154 NotificationListener listener; 155 Notification notification; 156 Object handback; 157 public AsynchNotifier(NotificationListener listener, 158 Notification notification, 159 Object handback) 160 { 161 this.listener = listener; 162 this.notification = notification; 163 this.handback = handback; 164 } 165 166 public void run() 167 { 168 try 169 { 170 listener.handleNotification(notification, handback); 171 } 172 catch (Throwable throwable) 173 { 174 log.error("Error processing notification=" + notification + 175 " listener=" + listener + 176 " handback=" + handback, 177 throwable); 178 } 179 } 180 } 181 } 182 | Popular Tags |