KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > notification > AsynchNotificationBroadcasterSupport


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.notification;
23
24 import javax.management.Notification JavaDoc;
25 import javax.management.NotificationListener JavaDoc;
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 /**
33  * A notification broadcaster with asynch notifications
34  *
35  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
36  * @author Scott.Stark@jboss.org
37  * @version $Revision: 37459 $
38  */

39 public class AsynchNotificationBroadcasterSupport
40    extends JBossNotificationBroadcasterSupport
41 {
42    // Attributes ----------------------------------------------------
43
private static Logger log = Logger.getLogger(AsynchNotifier.class);
44    /** The default pool used in the absence of on instance specific one */
45    private static ThreadPool defaultPool = new BasicThreadPool("AsynchNotificationBroadcasterSupport");
46    /** The default comp*/
47    private static long defaultNotificationTimeout;
48
49    /** The instance */
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    // Constructor ---------------------------------------------------
68

69    /**
70     * Construct a new Asyncrhonous broadcaster
71     * Calls this(defaultNotificationTimeout, defaultPool)
72     */

73    public AsynchNotificationBroadcasterSupport()
74    {
75       this(defaultNotificationTimeout, defaultPool);
76    }
77    /**
78     * Construct a new Asyncrhonous broadcaster. Calls
79     * this(notificationTimeout, defaultPool)
80     * @param notificationTimeout the notification completion timeout in MS. A
81     * 0 value means no timeout.
82     */

83    public AsynchNotificationBroadcasterSupport(long notificationTimeout)
84    {
85       this(notificationTimeout, defaultPool);
86    }
87    /**
88     * Construct a new Asyncrhonous broadcaster
89     * @param notificationTimeout - the notification completion timeout in MS. A
90     * 0 value means no timeout.
91     * @param pool - the thread pool to use for the asynchronous notifcations
92     */

93    public AsynchNotificationBroadcasterSupport(long notificationTimeout,
94       ThreadPool pool)
95    {
96       this.notificationTimeout = notificationTimeout;
97       this.pool = pool;
98    }
99
100    // Public --------------------------------------------------------
101

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

122    /**
123     * Handle the notification, asynchronously invoke the listener.
124     *
125     * @param listener the listener to notify
126     * @param notification the notification
127     * @param handback the handback object
128     */

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

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

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