KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > work > WorkExecutorPoolImpl


1 /*
2  * $Id: WorkExecutorPoolImpl.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 /**
12  *
13  * Copyright 2004 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */

27
28 package org.mule.impl.work;
29
30 import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue;
31 import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
32 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
33
34 import org.mule.config.ThreadingProfile;
35 import org.mule.util.concurrent.WaitPolicy;
36
37 /**
38  * Based class for WorkExecutorPool. Sub-classes define the synchronization policy
39  * (should the call block until the end of the work; or when it starts et cetera).
40  *
41  * @version $Rev: 3798 $ $Date: 2006-11-03 22:07:14 -0600 (Fri, 03 Nov 2006) $
42  */

43 public class WorkExecutorPoolImpl implements WorkExecutorPool
44 {
45
46     /**
47      * A timed out pooled executor.
48      */

49     private ThreadPoolExecutor pooledExecutor;
50
51     private ThreadingProfile profile;
52
53     private String JavaDoc name;
54
55     private static final long SHUTDOWN_TIMEOUT = 5000L;
56
57     /**
58      * Creates a pool with the specified minimum and maximum sizes. The Channel used
59      * to enqueue the submitted Work instances is queueless synchronous one.
60      *
61      * @param profile The threading profile to use when creating a pool for this work
62      * manager
63      * @param name The name to associate with the threads created in this pool
64      */

65     public WorkExecutorPoolImpl(ThreadingProfile profile, String JavaDoc name)
66     {
67         this.profile = profile;
68         this.name = name;
69         pooledExecutor = profile.createPool(name);
70     }
71
72     /**
73      * Creates a pool with the specified minimum and maximum sizes and using the
74      * specified Channel to enqueue the submitted Work instances.
75      *
76      * @param queue Queue to be used as the queueing facility of this pool.
77      * @param maxSize Maximum size of the work executor pool.
78      */

79     public WorkExecutorPoolImpl(BlockingQueue queue, int maxSize)
80     {
81         pooledExecutor = new ThreadPoolExecutor(0, maxSize, 60L, TimeUnit.SECONDS, queue);
82         pooledExecutor.setCorePoolSize(maxSize);
83         pooledExecutor.setRejectedExecutionHandler(new WaitPolicy(
84             ThreadingProfile.DEFAULT_THREAD_WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
85     }
86
87     /**
88      * Execute the specified Work.
89      *
90      * @param work Work to be executed.
91      * @exception InterruptedException Indicates that the Work execution has been
92      * unsuccessful.
93      */

94     public void execute(Runnable JavaDoc work)
95     {
96         pooledExecutor.execute(work);
97     }
98
99     /**
100      * Gets the size of this pool.
101      */

102     public int getPoolSize()
103     {
104         return pooledExecutor.getPoolSize();
105     }
106
107     /**
108      * Gets the maximum size of this pool.
109      */

110     public int getMaximumPoolSize()
111     {
112         return pooledExecutor.getMaximumPoolSize();
113     }
114
115     /**
116      * Sets the maximum size of this pool.
117      *
118      * @param maxSize New maximum size of this pool.
119      */

120     public void setMaximumPoolSize(int maxSize)
121     {
122         pooledExecutor.setMaximumPoolSize(maxSize);
123     }
124
125     public WorkExecutorPool start()
126     {
127         throw new IllegalStateException JavaDoc("This pooled executor is already started");
128     }
129
130     /**
131      * Stops this pool. Prior to stop this pool, all the enqueued Work instances are
132      * processed, if possible, in the allowed timeout. After what, all threads are
133      * interrupted and waited for. This is an mix orderly / abrupt shutdown.
134      */

135     public WorkExecutorPool stop()
136     {
137         pooledExecutor.shutdownNow();
138         try
139         {
140             pooledExecutor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
141         }
142         catch (InterruptedException JavaDoc e)
143         {
144             // Continue
145
}
146         return new NullWorkExecutorPool(profile, name);
147     }
148
149 }
150
Popular Tags