KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > AsyncProcessor


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: AsyncProcessor.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import org.opensubsystems.core.error.OSSException;
28
29 /**
30  * Thread used to asynchronously process items added to the queue. The items
31  * are processesed in the order as the are added to the queue and the thread
32  * blocks if the queue is empty.
33  *
34  * The class, which wants to perform async data processing will usually inline
35  * derive new class and define method processIte.
36  *
37  * This class cannot be abstract so that we can do the in place overriding using
38  * anonymous classes.
39  *
40  * @version $Id: AsyncProcessor.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
41  * @author Miro Halas
42  * @code.reviewer Miro Halas
43  * @code.reviewed 1.2 2004/12/18 06:18:25 bastafidli
44  */

45 public class AsyncProcessor extends Thread JavaDoc
46 {
47    // Attributes ///////////////////////////////////////////////////////////////
48

49    /**
50     * Synchronized queue use to keep data which should be processed asynchroneously.
51     * It is also used to block the thread if there is nothing to process.
52     */

53    protected SynchronizedQueue m_syncQueue;
54
55    /**
56     * By setting this flag to false we can stop the thread.
57     */

58    protected boolean m_bKeepRunning;
59
60    // Cached values ////////////////////////////////////////////////////////////
61

62    /**
63     * Logger for this class
64     */

65    private static Logger JavaDoc s_logger = Log.getInstance(AsyncProcessor.class);
66
67    // Constructors /////////////////////////////////////////////////////////////
68

69    /**
70     * Default constructor
71     *
72     * @param strName - name of this processor
73     */

74    public AsyncProcessor(
75       String JavaDoc strName
76    )
77    {
78       super(strName);
79       
80       m_syncQueue = new SynchronizedQueue();
81       m_bKeepRunning = true;
82    }
83    
84    // Logic ////////////////////////////////////////////////////////////////////
85

86    /**
87     * Method called during thread execution.
88     */

89    public void run(
90    )
91    {
92       Object JavaDoc itemToProcess;
93       
94       while (m_bKeepRunning)
95       {
96          try
97          {
98             // This call will block if there is no item to process
99
itemToProcess = m_syncQueue.get();
100             
101             // This is invalid assumption since we can put null items to the queue
102
// assert itemToProcess != null : "Synchronized queue cannot return null item";
103

104             if (itemToProcess != null)
105             {
106                s_logger.log(Level.FINEST, getName()
107                                      + ": Going to process " + itemToProcess.toString());
108             }
109             else
110             {
111                s_logger.log(Level.FINEST, getName()
112                                      + ": Going to process null object.");
113             }
114             processItemNow(itemToProcess);
115          }
116          catch (InterruptedException JavaDoc ieExc)
117          {
118             // The thread was release from block even though there was no item
119
// to process
120
}
121          catch (Throwable JavaDoc thr)
122          {
123             // Catch the exception inside of the loop to prevent the thread from
124
// dying. THe thread should be a deamon thread.
125
s_logger.log(Level.WARNING, getName()
126                                   + ": Unexpected error has occured while processing data.",
127                                   thr);
128          }
129       }
130    }
131    
132    /**
133     * Stop processing done by this thread as soon as possible
134     *
135     */

136    public void stopProcessing(
137    )
138    {
139       // TODO: Improve: This may not be completely correct, there might be race
140
// condition between while and get() and call of this method. Hopefully
141
// most threads will be deamons so solve it later.
142
m_bKeepRunning = false;
143       // The thread may be blocked so we need to interrupt it
144
interrupt();
145    }
146    
147    /**
148     * Add item to the queue to be processed later.
149     * This method is called synchroneously, the item is put into the queue,
150     * the calling thread returns then the item is taken out of the queue by
151     * this thread and overriden processItemNow method is called to process it.
152     *
153     * @param objItem - item to process, this might be null if null was put into
154     * the queue
155     */

156    public void processItemLater(
157       Object JavaDoc objItem
158    )
159    {
160       // This make wake up the thread above
161
if (objItem != null)
162       {
163          s_logger.log(Level.FINEST, getName()
164                                + ": Adding object to the queue " + objItem.toString());
165       }
166       else
167       {
168          s_logger.log(Level.FINEST, getName()
169                                + ": Adding null object to the queue.");
170       }
171       m_syncQueue.put(objItem);
172    }
173    
174    /**
175     * Process item which was placed into the queue in processItemLater.
176     * This method is called asynchroneously, the item is put into the queue,
177     * the calling thread returns then the item is taken out of the queue by
178     * this thread and this method is called to process it.
179     *
180     * This method cannot be abstract so that we can do the in place overriding
181     * of the class.
182     *
183     * @param objItem - item to process, this might be null if null was put into
184     * the queue
185     * @throws OSSException - an error has occured
186     */

187    protected void processItemNow(
188       Object JavaDoc objItem
189    ) throws OSSException
190    {
191       throw new UnsupportedOperationException JavaDoc("This method must be overriden.");
192    }
193 }
194
Popular Tags