View Javadoc

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-2002 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: ThreadPoolManager.java,v 1.4 2003/08/07 13:33:10 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 11/23/00	    jima    Created
47   */
48  package org.exolab.jms.threads;
49  
50  // core java
51  
52  import java.util.HashMap;
53  
54  import org.exolab.core.service.BasicService;
55  import org.exolab.core.service.ServiceException;
56  import org.exolab.core.service.ServiceState;
57  import org.exolab.core.threadPool.ThreadPool;
58  
59  
60  /***
61   * The thread pool manager manages all the required {@link ThreadPool}
62   * objects.
63   * The clients can obtain a ThreadPool from the {@link ThreadPoolManager} with
64   * a given number of Threads.
65   * <p>
66   * The manager keeps a list of all The ThreadPools it has dished out with a
67   * unique client provided name. Client can then re-request the already created
68   * ThreadPool or share common ThreadPools if required.
69   * <p>
70   * The ThreadPool manager will attempt to shutdown all ThreadPools and
71   * stop all Threads when it receives a stop request.
72   * <p>
73   * If a client attempts to create a ThreadPool with a name that already
74   * exists a ThreadPoolExistsException will be raised and the creation
75   * will fail.
76   *
77   * @version     $Revision: 1.4 $ $Date: 2003/08/07 13:33:10 $
78   * @author      <a href="mailto:mourikis@intalio.com">Jim Mourikis</a>
79   **/
80  public class ThreadPoolManager extends BasicService {
81  
82      // The list of all allocated ThreadPools.
83      private static HashMap _pools = null;
84  
85      // The one and only instance of this class.
86      private static ThreadPoolManager _instance = null;
87  
88      // The thread we run in.
89      private static final String THREADPOOLMGR_NAME = "ThreadPoolManager";
90  
91  
92      /***
93       * The constructor initialises the parent, and creates an empty container
94       * to hold the thread pools, as they are requested.
95       *
96       * <P> Entry into this constructor is only through the intialise method
97       * below. Note only one instance of this mgr is created.
98       */
99      private ThreadPoolManager() {
100         super(THREADPOOLMGR_NAME);
101         _pools = new HashMap();
102     }
103 
104     /***
105      * Return the one and only instance of the ThreadPoolManager.
106      *
107      * <P>Note: Initiase must be called above to initialise the pool, else
108      * a null object will be returned.
109      */
110     static public ThreadPoolManager instance() {
111         if (_instance == null) {
112             _instance = new ThreadPoolManager();
113         }
114         return _instance;
115     }
116 
117     /***
118      * A client has requested a new thread pool creation with the given name.
119      * if the named Pool has not already been created, then create it
120      * add it to the list, and return it to the client.
121      *
122      * @param name The unique name given to this ThreadPool
123      * @param size The maximum nuber of Threads this pool contains.
124      * @throws ThreadPoolExistsException if the pool with the given name
125      * already exists.
126      */
127     public ThreadPool createThreadPool(String name, int size)
128         throws ThreadPoolExistsException {
129         synchronized (_pools) {
130             if (_pools.containsKey(name)) {
131                 throw new ThreadPoolExistsException("ThreadPool with name " +
132                     name + " exists");
133             }
134             ThreadPool pool = new ThreadPool(name, size, true);
135 
136             _pools.put(name, pool);
137             return pool;
138         }
139     }
140 
141     /***
142      * Get the ThreadPool with the given name. if the named pool does not exist
143      * throw an exception.
144      *
145      * @param name The unique name of the requested ThreadPool
146      * @throws UnknownThreadPoolException if the pool with the given name
147      * does not exist.
148      */
149     public ThreadPool getThreadPool(String name)
150         throws UnknownThreadPoolException {
151         ThreadPool pool = null;
152 
153         synchronized (_pools) {
154             pool = (ThreadPool) _pools.get(name);
155         }
156 
157         if (pool == null) {
158             throw new UnknownThreadPoolException("ThreadPool with name " +
159                 name + " does not exist");
160         }
161 
162         return pool;
163     }
164 
165     /***
166      * Attempts to shutdown all the threads in the pool and removes the given
167      * ThreadPool.
168      *
169      * @param name The unique name of the requested ThreadPool
170      * @throws  UnknownThreadPoolException if the pool with the given name
171      * does not exist.
172      */
173     public void deleteThreadPool(String name)
174         throws UnknownThreadPoolException {
175         ThreadPool pool = null;
176 
177         synchronized (_pools) {
178             pool = (ThreadPool) _pools.remove(name);
179         }
180 
181         if (pool == null) {
182             throw new UnknownThreadPoolException("ThreadPool with name " +
183                 name + " does not exist");
184         }
185         pool.stopRequestAllWorkers();
186     }
187 
188     /***
189      * This starts the ThreadPoolManager's thread
190      */
191     public synchronized void run() {
192         while (getState() != ServiceState.STOPPED) {
193             try {
194                 // wait for the sevice to terminate.
195                 wait();
196             } catch (InterruptedException ignore) {
197             }
198         }
199     }
200 
201     /***
202      * Overide BasicService.stop. Clean up all thread pools,
203      * by calling their stop methods.
204      *
205      * @throws ServiceException
206      */
207     public void stop() throws ServiceException {
208         synchronized (_pools) {
209             Object[] ob = _pools.values().toArray();
210 
211             for (int i = 0, j = _pools.size(); i < j; i++) {
212                 ((ThreadPool) ob[i]).stopRequestAllWorkers();
213             }
214             _pools.clear();
215         }
216         super.stop();
217         // zero out the static reference
218         _pools = null;
219         _instance = null;
220     }
221 
222 } //-- ThreadPoolManager