KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > framework > server > AsynchEventHandler


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.ha.framework.server;
23
24 import org.jboss.logging.Logger;
25
26 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
27
28 /**
29  * Utility class that accepts objects into a queue and maintains a separate
30  * thread that reads them off the queue and passes them to a registered
31  * "processor".
32  *
33  * @todo find a better home for this than the cluster module
34  *
35  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
36  * @version $Revision$
37  */

38 class AsynchEventHandler implements Runnable JavaDoc
39 {
40    /**
41     * Interface implemented by classes able to process the objects
42     * placed into an AsynchEventHandler's queue.
43     */

44    public static interface AsynchEventProcessor
45    {
46       public void processEvent(Object JavaDoc event);
47    }
48    
49    private String JavaDoc name;
50    /** The LinkedQueue of events to pass to our processor */
51    private LinkedQueue events = new LinkedQueue();
52    /** Whether we're blocking on the queue */
53    private boolean blocking;
54    private AsynchEventProcessor processor;
55    private boolean stopped = true;
56    private Thread JavaDoc handlerThread;
57    private Logger log;
58    
59    /**
60     * Create a new AsynchEventHandler.
61     *
62     * @param processor object to which objects placed in the queue should
63     * be handed when dequeued
64     * @param name name for this instance. Appended to the processor's
65     * class name to create a log category, and used
66     * to name to handler thread
67     */

68    public AsynchEventHandler(AsynchEventProcessor processor, String JavaDoc name)
69    {
70       super();
71       this.processor = processor;
72       if (name == null)
73          name = "AsynchEventHandler";
74       this.name = name;
75       this.log = Logger.getLogger(processor.getClass().getName() + "." + name);
76    }
77    
78    /**
79     * Place the given object in the queue.
80     *
81     * @param event the object to asynchronously pass to the
82     * AsynchEventHandler.
83     *
84     * @throws InterruptedException if the thread is interrupted while blocking
85     * on the queue.
86     */

87    public void queueEvent(Object JavaDoc event) throws InterruptedException JavaDoc
88    {
89       if (event != null)
90          events.put(event);
91    }
92    
93    public void run()
94    {
95       log.debug("Begin " + name + " Thread");
96       stopped = false;
97       while( !stopped )
98       {
99          try
100          {
101             blocking = true;
102             Object JavaDoc event = events.take();
103             blocking = false;
104             
105             if (!stopped)
106             {
107                processor.processEvent(event);
108             }
109          }
110          catch(InterruptedException JavaDoc e)
111          {
112             blocking = false;
113             if (stopped)
114             {
115                log.debug(name + " Thread interrupted");
116                break;
117             }
118             log.error(name + " Thread interrupted", e);
119          }
120          catch (Throwable JavaDoc t)
121          {
122             log.error("Caught Throwable handling asynch events", t);
123          }
124       }
125       log.debug("End " + name + " Thread");
126    }
127    
128    /**
129     * Starts the handler thread.
130     */

131    public void start()
132    {
133       handlerThread = new Thread JavaDoc(this, name + " Thread");
134       handlerThread.start();
135    }
136    
137    /**
138     * Stops the handler thread.
139     */

140    public void stop()
141    {
142       stopped = true;
143       if (blocking)
144          handlerThread.interrupt(); // it's just waiting on the LinkedQueue
145

146       if (handlerThread.isAlive()) {
147          // Give it up to 100ms to finish whatever it's doing
148
try
149          {
150             handlerThread.join(100);
151          }
152          catch (Exception JavaDoc ignored) {}
153       }
154       
155       if (handlerThread.isAlive())
156          handlerThread.interrupt(); // kill it
157
}
158    
159    public boolean isStopped()
160    {
161       return stopped;
162    }
163
164 }
165
Popular Tags