KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > threads > ThreadPoolManager


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ThreadPoolManager.java,v 1.1 2004/11/26 01:51:01 tanderson Exp $
44  */

45 package org.exolab.jms.threads;
46
47 import java.util.HashMap JavaDoc;
48
49 import org.exolab.jms.service.BasicService;
50 import org.exolab.jms.service.ServiceException;
51 import org.exolab.jms.service.ServiceState;
52 import org.exolab.jms.common.threads.ThreadPool;
53
54
55 /**
56  * The thread pool manager manages all the required {@link ThreadPool}
57  * objects.
58  * The clients can obtain a ThreadPool from the {@link ThreadPoolManager} with
59  * a given number of Threads.
60  * <p>
61  * The manager keeps a list of all The ThreadPools it has dished out with a
62  * unique client provided name. Client can then re-request the already created
63  * ThreadPool or share common ThreadPools if required.
64  * <p>
65  * The ThreadPool manager will attempt to shutdown all ThreadPools and
66  * stop all Threads when it receives a stop request.
67  * <p>
68  * If a client attempts to create a ThreadPool with a name that already
69  * exists a ThreadPoolExistsException will be raised and the creation
70  * will fail.
71  *
72  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:01 $
73  * @author <a HREF="mailto:mourikis@intalio.com">Jim Mourikis</a>
74  **/

75 public class ThreadPoolManager extends BasicService {
76
77     // The list of all allocated ThreadPools.
78
private static HashMap JavaDoc _pools = null;
79
80     // The one and only instance of this class.
81
private static ThreadPoolManager _instance = null;
82
83     // The thread we run in.
84
private static final String JavaDoc THREADPOOLMGR_NAME = "ThreadPoolManager";
85
86
87     /**
88      * The constructor initialises the parent, and creates an empty container
89      * to hold the thread pools, as they are requested.
90      *
91      * <P> Entry into this constructor is only through the intialise method
92      * below. Note only one instance of this mgr is created.
93      */

94     private ThreadPoolManager() {
95         super(THREADPOOLMGR_NAME);
96         _pools = new HashMap JavaDoc();
97     }
98
99     /**
100      * Return the one and only instance of the ThreadPoolManager.
101      *
102      * <P>Note: Initiase must be called above to initialise the pool, else
103      * a null object will be returned.
104      */

105     static public ThreadPoolManager instance() {
106         if (_instance == null) {
107             _instance = new ThreadPoolManager();
108         }
109         return _instance;
110     }
111
112     /**
113      * A client has requested a new thread pool creation with the given name.
114      * if the named Pool has not already been created, then create it
115      * add it to the list, and return it to the client.
116      *
117      * @param name The unique name given to this ThreadPool
118      * @param size The maximum nuber of Threads this pool contains.
119      * @throws ThreadPoolExistsException if the pool with the given name
120      * already exists.
121      */

122     public ThreadPool createThreadPool(String JavaDoc name, int size)
123         throws ThreadPoolExistsException {
124         synchronized (_pools) {
125             if (_pools.containsKey(name)) {
126                 throw new ThreadPoolExistsException("ThreadPool with name " +
127                     name + " exists");
128             }
129             ThreadPool pool = new ThreadPool(name, size, true);
130
131             _pools.put(name, pool);
132             return pool;
133         }
134     }
135
136     /**
137      * Get the ThreadPool with the given name. if the named pool does not exist
138      * throw an exception.
139      *
140      * @param name The unique name of the requested ThreadPool
141      * @throws UnknownThreadPoolException if the pool with the given name
142      * does not exist.
143      */

144     public ThreadPool getThreadPool(String JavaDoc name)
145         throws UnknownThreadPoolException {
146         ThreadPool pool = null;
147
148         synchronized (_pools) {
149             pool = (ThreadPool) _pools.get(name);
150         }
151
152         if (pool == null) {
153             throw new UnknownThreadPoolException("ThreadPool with name " +
154                 name + " does not exist");
155         }
156
157         return pool;
158     }
159
160     /**
161      * Attempts to shutdown all the threads in the pool and removes the given
162      * ThreadPool.
163      *
164      * @param name The unique name of the requested ThreadPool
165      * @throws UnknownThreadPoolException if the pool with the given name
166      * does not exist.
167      */

168     public void deleteThreadPool(String JavaDoc name)
169         throws UnknownThreadPoolException {
170         ThreadPool pool = null;
171
172         synchronized (_pools) {
173             pool = (ThreadPool) _pools.remove(name);
174         }
175
176         if (pool == null) {
177             throw new UnknownThreadPoolException("ThreadPool with name " +
178                 name + " does not exist");
179         }
180         pool.stopRequestAllWorkers();
181     }
182
183     /**
184      * This starts the ThreadPoolManager's thread
185      */

186     public synchronized void run() {
187         while (getState() != ServiceState.STOPPED) {
188             try {
189                 // wait for the sevice to terminate.
190
wait();
191             } catch (InterruptedException JavaDoc ignore) {
192             }
193         }
194     }
195
196     /**
197      * Overide BasicService.stop. Clean up all thread pools,
198      * by calling their stop methods.
199      *
200      * @throws ServiceException
201      */

202     public void stop() throws ServiceException {
203         synchronized (_pools) {
204             Object JavaDoc[] ob = _pools.values().toArray();
205
206             for (int i = 0, j = _pools.size(); i < j; i++) {
207                 ((ThreadPool) ob[i]).stopRequestAllWorkers();
208             }
209             _pools.clear();
210         }
211         super.stop();
212         // zero out the static reference
213
_pools = null;
214         _instance = null;
215     }
216
217 } //-- ThreadPoolManager
218
Popular Tags