KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > ExecutorServicePipeline


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.web.connector.grizzly;
24
25 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
26 import java.util.concurrent.ThreadPoolExecutor JavaDoc;
27 import java.util.concurrent.TimeUnit JavaDoc;
28
29 /**
30  * A wrapper around an <code>ThreadPoolExecutor</code>.
31  *
32  * @author Jean-Francois Arcand
33  */

34 public class ExecutorServicePipeline implements Pipeline {
35     
36     /**
37      * The number of thread waiting for a <code>Task</code>
38      */

39     private int waitingThreads = 0;
40     
41     
42     /**
43      * The maximum number of Thread
44      */

45     private int maxThreads = 50;
46     
47
48     /**
49      * The minimum numbers of <code>WorkerThread</code>
50      */

51     private int minThreads = 10;
52     
53     /**
54      * The minimum numbers of spare <code>WorkerThread</code>
55      */

56     private int minSpareThreads = 5;
57
58     
59     /**
60      * The port used.
61      */

62     private int port = 8080;
63     
64
65     /**
66      * The number of <code>WorkerThread</code>
67      */

68     private int threadCount =0;
69     
70
71     /**
72      * The name of this Pipeline
73      */

74     private String JavaDoc name;
75     
76     
77     /**
78      * The Thread Priority
79      */

80     private int priority = Thread.NORM_PRIORITY;
81     
82     
83     /**
84      * Has the pipeline already started
85      */

86     private boolean isStarted = false;
87     
88     
89     /**
90      * <code>ExecutorService</code> wrapped by this pipeline.
91      */

92     private ThreadPoolExecutor JavaDoc workerThreads;
93     
94      
95     /**
96      * The <code>PipelineStatistic</code> objects used when gathering statistics.
97      */

98     protected PipelineStatistic pipelineStat;
99     
100     
101     /**
102      * Maximum pending connection before refusing requests.
103      */

104     private int maxQueueSizeInBytes = -1;
105     // ------------------------------------------------ Lifecycle ------------/
106

107     /**
108      * Init the <code>Pipeline</code> by initializing the required
109      * <code>ThreadPoolExecutor</code>.
110      */

111     public void initPipeline(){
112         if (isStarted){
113             return;
114         }
115         isStarted = true;
116         
117         if (maxThreads < minSpareThreads){
118             maxThreads = minSpareThreads;
119         }
120         workerThreads = new ThreadPoolExecutor JavaDoc(
121                                maxThreads,
122                                maxThreads,
123                                0L,
124                                TimeUnit.MILLISECONDS,
125                                new LinkedBlockingQueue JavaDoc<Runnable JavaDoc>(),
126                                new GrizzlyThreadFactory(name,port,priority));
127     }
128
129     /**
130      * Start the <code>Pipeline</code>
131      */

132     public void startPipeline(){
133         if (isStarted){
134             return;
135         }
136         ; // Do nothing
137
}
138     
139
140     /**
141      * Stop the <code>Pipeline</code>
142      */

143     public void stopPipeline(){
144         if (!isStarted){
145             return;
146         }
147         isStarted = false;
148         workerThreads.shutdown();
149     }
150
151     
152     /**
153      * Interrupt the <code>Thread</code> using it thread id
154      */

155     public boolean interruptThread(long threadID){
156         return ((GrizzlyThreadFactory)workerThreads.getThreadFactory())
157             .interruptThread(threadID);
158     }
159     // ---------------------------------------------------- Queue ------------//
160

161     
162     /**
163      * Add an object to this pipeline
164      */

165     public void addTask(Task task){
166         if (workerThreads.getQueue().size() > maxQueueSizeInBytes ){
167             task.cancelTask("Maximum Connections Reached: "
168                             + pipelineStat.getQueueSizeInBytes()
169                             + " -- Retry later", HtmlHelper.OK);
170             task.getSelectorThread().returnTask(task);
171             return;
172         }
173         workerThreads.execute((Runnable JavaDoc)task);
174               
175         if ( pipelineStat != null) {
176             // Add one to include the current requests
177
pipelineStat.gather(size());
178         }
179     }
180
181
182     /**
183      * Return a <code>Task</code> object available in the pipeline.
184      *
185      */

186     public Task getTask() {
187         return null;
188     }
189
190     
191     /**
192      * Returns the number of tasks in this <code>Pipeline</code>.
193      *
194      * @return Number of tasks in this <code>Pipeline</code>.
195      */

196     public int size() {
197         return workerThreads.getQueue().size();
198     }
199
200
201     // --------------------------------------------------Properties ----------//
202

203     /**
204      * Return the number of waiting threads.
205      */

206     public int getWaitingThread(){
207         return workerThreads.getPoolSize() - workerThreads.getActiveCount();
208     }
209     
210     
211     /**
212      * Set the number of threads used by this pipeline.
213      */

214     public void setMaxThreads(int maxThreads){
215         this.maxThreads = maxThreads;
216     }
217     
218     
219     /**
220      * Return the number of threads used by this pipeline.
221      */

222     public int getMaxThreads(){
223         return maxThreads;
224     }
225     
226     
227     /**
228      * Return the current number of threads used.
229      */

230     public int getCurrentThreadCount() {
231         return workerThreads.getPoolSize() ;
232     }
233       
234       
235     /**
236      * Return the curent number of threads that are currently processing
237      * a task.
238      */

239     public int getCurrentThreadsBusy(){
240         return workerThreads.getActiveCount();
241     }
242     
243     
244     /**
245      * Return the maximum spare thread.
246      */

247     public int getMaxSpareThreads() {
248         return getWaitingThread();
249     }
250     
251     
252     /**
253      * Return the minimum spare thread.
254      */

255     public int getMinSpareThreads() {
256         return 0;
257     }
258     
259     
260     /**
261      * Set the minimum space thread this <code>Pipeline</code> can handle.
262      */

263     public void setMinSpareThreads(int minSpareThreads) {
264         this.minSpareThreads = minSpareThreads;
265     }
266
267     
268     /**
269      * Set the thread priority of the <code>Pipeline</code>
270      */

271     public void setPriority(int priority){
272         this.priority = priority;
273     }
274     
275     
276     /**
277      * Set the name of this <code>Pipeline</code>
278      */

279     public void setName(String JavaDoc name){
280         this.name = name;
281     }
282     
283     
284     /**
285      * Return the name of this <code>Pipeline</code>
286      * @return the name of this <code>Pipeline</code>
287      */

288     public String JavaDoc getName(){
289         return name+port;
290     }
291
292     
293     /**
294      * Set the port used by this <code>Pipeline</code>
295      * @param port the port used by this <code>Pipeline</code>
296      */

297     public void setPort(int port){
298         this.port = port;
299     }
300     
301     
302     /**
303      * Set the minimum thread this <code>Pipeline</code> will creates
304      * when initializing.
305      * @param minThreads the minimum number of threads.
306      */

307     public void setMinThreads(int minThreads){
308         this.minThreads = minThreads;
309     }
310     
311     
312     public String JavaDoc toString(){
313        return "name: " + name + " maxThreads: " + maxThreads
314                 + " minThreads:" + minThreads;
315     }
316     
317
318     /**
319      * Set the maximum pending connection this <code>Pipeline</code>
320      * can handle.
321      */

322     public void setQueueSizeInBytes(int maxQueueSizeInBytes){
323         this.maxQueueSizeInBytes = maxQueueSizeInBytes;
324         if ( pipelineStat != null )
325             pipelineStat.setQueueSizeInBytes(maxQueueSizeInBytes);
326     }
327     
328     
329     /**
330      * Get the maximum pending connection this <code>Pipeline</code>
331      * can handle.
332      */

333     public int getQueueSizeInBytes(){
334         return maxQueueSizeInBytes;
335     }
336     
337     
338     public void setThreadsIncrement(int threadsIncrement){
339         ; // Not Supported
340
}
341     
342     
343     public void setThreadsTimeout(int threadsTimeout){
344         ; // Not Supported
345
}
346
347     
348     /**
349      * Set the <code>PipelineStatistic</code> object used
350      * to gather statistic;
351      */

352     public void setPipelineStatistic(PipelineStatistic pipelineStatistic){
353         this.pipelineStat = pipelineStatistic;
354     }
355     
356     
357     /**
358      * Return the <code>PipelineStatistic</code> object used
359      * to gather statistic;
360      */

361     public PipelineStatistic getPipelineStatistic(){
362         return pipelineStat;
363     }
364     
365 }
366
Popular Tags