KickJava   Java API By Example, From Geeks To Geeks.

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


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.nio.channels.SelectionKey JavaDoc;
26 import java.util.List JavaDoc;
27
28 import java.util.concurrent.ConcurrentHashMap JavaDoc;
29 import java.util.concurrent.ConcurrentLinkedQueue JavaDoc;
30 import java.util.concurrent.ScheduledThreadPoolExecutor JavaDoc;
31 import java.util.concurrent.TimeUnit JavaDoc;
32
33
34 /**
35  * Keep Alive subsystems. This class will cancel <code>SelectionKey<code>
36  * based on the maxKeepAliveRequests.
37  *
38  * @author Jeanfrancois Arcand
39  */

40 public class KeepAlivePipeline implements Pipeline{
41     
42     
43     public final static int KEEP_ALIVE_RULE = 0;
44     
45     /**
46      * The maximum number of Thread
47      */

48     private int maxThreads = 1;
49    
50     
51     /**
52      * The Thread Priority
53      */

54     private int priority = Thread.NORM_PRIORITY;
55     
56     
57     /**
58      * The port used.
59      */

60     private int port = 8080;
61         
62
63     /**
64      * The name of this Pipeline
65      */

66     private String JavaDoc name;
67     
68     
69     /**
70      * Has the pipeline already started
71      */

72     private boolean isStarted = false;
73     
74     
75     /*
76      * Number of seconds before idle accepted connections expire.
77      * Default is 30 seconds as per domain.xml DTD.
78      */

79     private int firstReadTimeout = Constants.DEFAULT_TIMEOUT;
80     
81     /*
82      * Number of seconds before idle keep-alive connections expire.
83      * Default is 30 seconds as per domain.xml DTD.
84      */

85     private int keepAliveTimeoutInSeconds = Constants.DEFAULT_TIMEOUT;
86     
87     
88     /**
89      * Placeholder for keep-alive count monitoring.
90      */

91     protected ConcurrentHashMap JavaDoc<SelectionKey JavaDoc,Integer JavaDoc> keepAliveCounts;
92     
93     
94     /**
95      * Maximum number of requests in a single transaction.
96      */

97     protected int maxKeepAliveRequests = -1;
98     
99     
100     /**
101      * The <code>PipelineStatistic</code> objects used when gathering statistics.
102      */

103     protected PipelineStatistic pipelineStat;
104       
105     
106     /**
107      * The stats object used to gather statistics.
108      */

109     private KeepAliveStats keepAliveStats;
110
111
112     // ----------------------------------------------- Constructor ----------//
113

114     /**
115      * Default constructor.
116      */

117     public KeepAlivePipeline(){
118         initPipeline();
119     }
120    
121     // ------------------------------------------------ Lifecycle ------------/
122

123     /**
124      * Init the <code>Pipeline</code> by initializing the required
125      * <code>WorkerThread</code>.
126      */

127     public void initPipeline(){
128         if (isStarted){
129             return;
130         }
131         isStarted = true;
132         keepAliveCounts = new ConcurrentHashMap JavaDoc<SelectionKey JavaDoc,Integer JavaDoc>();
133     }
134
135     
136     /**
137      * Start the <code>Pipeline</code> and all associated
138      * <code>WorkerThread</code>
139      */

140     public void startPipeline(){
141         if (isStarted){
142             return;
143         }
144         ; // Do nothing
145
}
146     
147
148     /**
149      * Stop the <code>Pipeline</code> and all associated
150      * <code>WorkerThread</code>
151      */

152     public void stopPipeline(){
153         if (!isStarted){
154             return;
155         }
156         isStarted = false;
157         keepAliveCounts.clear();
158     }
159     
160     // ---------------------------------------------------- Queue ------------//
161

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

166     public void addTask(Task task){
167         throw new UnsupportedOperationException JavaDoc();
168     }
169
170
171     /**
172      * Return a <code>SelectionKey</code> object available in the pipeline.
173      * All Threads will synchronize on that method
174      * @Return null This pipeline doesn't supports this method.
175      */

176     public Task getTask() {
177         return null;
178     }
179     
180
181     /**
182      * Returns the number of tasks in this <code>Pipeline</code>.
183      *
184      * @return Number of tasks in this <code>Pipeline</code>.
185      */

186     public int size() {
187         return 0;
188     }
189
190     
191     // --------------------------------------------------Properties ----------//
192

193      /**
194      * Return the number of waiting threads.
195      */

196     public int getWaitingThread(){
197         return 0;
198     }
199     
200     
201     /**
202      * Set the number of threads used by this pipeline.
203      */

204     public void setMaxThreads(int maxThreads){
205         this.maxThreads = maxThreads;
206     }
207     
208     
209     /**
210      * Return the number of threads used by this pipeline.
211      */

212     public int getMaxThreads(){
213         return maxThreads;
214     }
215     
216     
217     /**
218      * Return the current number of active threads.
219      */

220     public int getCurrentThreadCount() {
221         return 1;
222     }
223     
224     
225     /**
226      * Return the current number of active threads.
227      */

228     public int getCurrentThreadsBusy() {
229         return 1;
230     }
231     
232     
233     /**
234      * Return the maximum spare thread.
235      */

236     public int getMaxSpareThreads() {
237         return 0;
238     }
239
240     
241     /**
242      * Set the thread priority of the <code>Pipeline</code>
243      */

244     public void setPriority(int priority){
245         this.priority = priority;
246     }
247     
248     
249     /**
250      * Set the name of this <code>Pipeline</code>
251      */

252     public void setName(String JavaDoc name){
253         this.name = name;
254     }
255     
256     
257     /**
258      * Return the name of this <code>Pipeline</code>
259      * @return the name of this <code>Pipeline</code>
260      */

261     public String JavaDoc getName(){
262         return name+port;
263     }
264
265     
266     /**
267      * Set the port used by this <code>Pipeline</code>
268      * @param port the port used by this <code>Pipeline</code>
269      */

270     public void setPort(int port){
271         this.port = port;
272     }
273     
274     
275     /**
276      * Set the minimum thread this <code>Pipeline</code> will creates
277      * when initializing.
278      * @param minThreads the minimum number of threads.
279      */

280     public void setMinThreads(int minThreads){
281         throw new UnsupportedOperationException JavaDoc();
282     }
283     
284     
285     public String JavaDoc toString(){
286        return "name: " + name + " maxThreads: " + maxThreads ;
287     }
288
289     
290     /**
291      * Set the maximum pending connection this <code>Pipeline</code>
292      * can handle.
293      */

294     public void setQueueSizeInBytes(int maxQueueSizeInBytes){
295         throw new UnsupportedOperationException JavaDoc();
296     }
297     
298     
299     public void setThreadsIncrement(int threadsIncrement){
300        throw new UnsupportedOperationException JavaDoc();
301     }
302     
303     
304     public void setThreadsTimeout(int threadsTimeout){
305         this.firstReadTimeout = threadsTimeout;
306     }
307
308
309     /**
310      * Return the minimum spare thread.
311      */

312     public int getMinSpareThreads() {
313         throw new UnsupportedOperationException JavaDoc();
314     }
315
316
317     /**
318      * Set the minimum space thread this <code>Pipeline</code> can handle.
319      */

320     public void setMinSpareThreads(int minSpareThreads) {
321         throw new UnsupportedOperationException JavaDoc();
322     }
323
324     
325     // ------------------------------------------------ maxKeepAliveRequests --/
326

327     /**
328      * Monitor keep-alive request count for the given connection.
329      * @return true if the request can be processed, false if the maximun
330      * number of requests has been reached.
331      */

332     public boolean trap(SelectionKey JavaDoc key){
333         if ( maxKeepAliveRequests == -1) return true;
334         
335         Integer JavaDoc count = keepAliveCounts.get(key);
336         if ( count == null ){
337             count = 0;
338             if (keepAliveStats != null) {
339                 keepAliveStats.incrementCountConnections();
340             }
341         }
342         
343         if ((count+1) > maxKeepAliveRequests){
344             if (keepAliveStats != null) {
345                 keepAliveStats.incrementCountRefusals();
346             }
347             return false;
348         }
349
350         keepAliveCounts.put(key, count++);
351         if (keepAliveStats != null) {
352             keepAliveStats.incrementCountHits();
353         }
354
355         return true;
356     }
357     
358     
359     /**
360      * Stop monitoring keep-alive request count for the given connection.
361      */

362     public void untrap(SelectionKey JavaDoc key){
363         if ( maxKeepAliveRequests == -1) return;
364         
365         keepAliveCounts.remove(key);
366     }
367     
368     
369     /**
370      * Return the maximum number of keep-alive requests per connection.
371      */

372     public int getMaxKeepAliveRequests() {
373         return maxKeepAliveRequests;
374     }
375     
376     
377     /**
378      * Set the maximum number of Keep-Alive requests that we will
379      * honor per connection. A value < 0 will disabled the keep-alive mechanism.
380      */

381     public void setMaxKeepAliveRequests(int maxKeepAliveRequests) {
382         this.maxKeepAliveRequests = maxKeepAliveRequests;
383     }
384     
385         
386     /**
387      * Sets the number of seconds before a keep-alive connection that has
388      * been idle times out and is closed.
389      *
390      * @param keepAliveTimeout Keep-alive timeout in number of seconds
391      */

392     public void setKeepAliveTimeoutInSeconds(int keepAliveTimeout){
393         this.keepAliveTimeoutInSeconds = keepAliveTimeout;
394     }
395                                                                                 
396                                                                                 
397     /**
398      * Gets the number of seconds before a keep-alive connection that has
399      * been idle times out and is closed.
400      *
401      * @return Keep-alive timeout in number of seconds
402      */

403     public int getKeepAliveTimeoutInSeconds(){
404         return keepAliveTimeoutInSeconds;
405     }
406     
407
408     /**
409      * Return <code>true</code> if we need to close the connection just after
410      * the first request.
411      */

412     public boolean dropConnection(){
413         return (keepAliveTimeoutInSeconds == 0
414                     || firstReadTimeout == 0 || maxKeepAliveRequests == 0);
415     }
416        
417     
418     
419     /**
420      * Set the <code>PipelineStatistic</code> object used
421      * to gather statistic;
422      */

423     public void setPipelineStatistic(PipelineStatistic pipelineStatistic){
424         this.pipelineStat = pipelineStatistic;
425     }
426     
427     
428     /**
429      * Return the <code>PipelineStatistic</code> object used
430      * to gather statistic;
431      */

432     public PipelineStatistic getPipelineStatistic(){
433         return pipelineStat;
434     }
435     
436
437     /**
438      * Sets the given KeepAliveStats, which is responsible for storing
439      * keep-alive statistics.
440      *
441      * @param keepAliveStats The KeepAliveStats object responsible for
442      * storing keep-alive statistics
443      */

444     void setKeepAliveStats(KeepAliveStats keepAliveStats) {
445         this.keepAliveStats = keepAliveStats;
446     }
447
448     
449     /**
450      * Interrupt the <code>Thread</code> using it thread id
451      */

452     public boolean interruptThread(long threadId){
453         return false;
454     }
455 }
456
457
Popular Tags