KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > core > notification > AsynchNotificationBroadcasterSupport


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package test.compliance.core.notification;
8
9 import javax.management.Notification JavaDoc;
10 import javax.management.NotificationListener JavaDoc;
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 /**
17  * A notification broadcaster with asynch notifications
18  *
19  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
20  * @author Scott.Stark@jboss.org
21  * @version $Revision: 33149 $
22  */

23 public class AsynchNotificationBroadcasterSupport
24       extends JBossNotificationBroadcasterSupport
25 {
26    // Attributes ----------------------------------------------------
27
private static Logger log = Logger.getLogger(AsynchNotifier.class);
28    /**
29     * The default pool used in the absence of on instance specific one
30     */

31    private static ThreadPool defaultPool = new BasicThreadPool("AsynchNotificationBroadcasterSupport");
32    /**
33     * The default comp
34     */

35    private static long defaultNotificationTimeout;
36
37    /**
38     * The instance
39     */

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    // Constructor ---------------------------------------------------
59

60    /**
61     * Construct a new Asyncrhonous broadcaster
62     * Calls this(defaultNotificationTimeout, defaultPool)
63     */

64    public AsynchNotificationBroadcasterSupport()
65    {
66       this(defaultNotificationTimeout, defaultPool);
67    }
68
69    /**
70     * Construct a new Asyncrhonous broadcaster. Calls
71     * this(notificationTimeout, defaultPool)
72     *
73     * @param notificationTimeout the notification completion timeout in MS. A
74     * 0 value means no timeout.
75     */

76    public AsynchNotificationBroadcasterSupport(long notificationTimeout)
77    {
78       this(notificationTimeout, defaultPool);
79    }
80
81    /**
82     * Construct a new Asyncrhonous broadcaster
83     *
84     * @param notificationTimeout - the notification completion timeout in MS. A
85     * 0 value means no timeout.
86     * @param pool - the thread pool to use for the asynchronous notifcations
87     */

88    public AsynchNotificationBroadcasterSupport(long notificationTimeout,
89                                                ThreadPool pool)
90    {
91       this.notificationTimeout = notificationTimeout;
92       this.pool = pool;
93    }
94
95    // Public --------------------------------------------------------
96

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    // NotificationBroadcasterSupport overrides ----------------------
118

119    /**
120     * Handle the notification, asynchronously invoke the listener.
121     *
122     * @param listener the listener to notify
123     * @param notification the notification
124     * @param handback the handback object
125     */

126    public void handleNotification(NotificationListener JavaDoc listener,
127                                   Notification JavaDoc notification,
128                                   Object JavaDoc handback)
129    {
130       AsynchNotifier notifier = new AsynchNotifier(listener, notification, handback);
131       pool.run(notifier, 0, notificationTimeout);
132    }
133
134    /**
135     * Invoke stop on the thread pool if its not the class default pool.
136     *
137     * @param immeadiate the immeadiate flag passed to the TheadPool#stop
138     */

139    protected void stopThreadPool(boolean immeadiate)
140    {
141       if(pool != defaultPool)
142       {
143          pool.stop(immeadiate);
144       }
145    }
146
147    // Inner classes -------------------------------------------------
148

149    public class AsynchNotifier
150          implements Runnable JavaDoc
151    {
152       NotificationListener JavaDoc listener;
153       Notification JavaDoc notification;
154       Object JavaDoc handback;
155
156       public AsynchNotifier(NotificationListener JavaDoc listener,
157                             Notification JavaDoc notification,
158                             Object JavaDoc 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 JavaDoc throwable)
172          {
173             log.error("Error processing notification=" + notification +
174                       " listener=" + listener +
175                       " handback=" + handback,
176                       throwable);
177          }
178       }
179    }
180 }
181
Popular Tags