KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.nio.channels.CancelledKeyException JavaDoc;
31 import java.nio.channels.ClosedChannelException JavaDoc;
32 import java.nio.channels.Selector JavaDoc;
33 import java.nio.channels.SelectionKey JavaDoc;
34 import java.nio.channels.SocketChannel JavaDoc;
35
36
37 /**
38  * Specialized <code>SelectorThread</code> that only handle OP_READ.
39  *
40  * @author Scott Oaks
41  * @author Jean-Francois Arcand
42  */

43 public class SelectorReadThread extends SelectorThread{
44
45     /**
46      * List of <code>Channel<code> to process.
47      */

48     ArrayList JavaDoc<SocketChannel JavaDoc> channels = new ArrayList JavaDoc<SocketChannel JavaDoc>();
49
50
51     /**
52      * Int used to differenciate thsi instance
53      */

54     public int countName;
55     
56
57     /**
58      * Init the <code>Pipeline</code>s used by the <code>WorkerThread</code>s.
59      */

60     protected void initPipeline(){
61         
62         initKeepAlivePipeline();
63         
64         processorPipeline = newPipeline(maxProcessorWorkerThreads,
65                                         minWorkerThreads, "http-SelectorRead-"
66                                         + countName + "-",
67                                         port,
68                                         Thread.MAX_PRIORITY);
69         processorPipeline.initPipeline();
70         
71         // Only creates the pipeline if the max > 0
72
if ( maxReadWorkerThreads > 0 ){
73             readPipeline = newPipeline(maxReadWorkerThreads,
74                                        minWorkerThreads, "read",
75                                        port,Thread.NORM_PRIORITY);
76             readPipeline.initPipeline();
77         } else {
78             readPipeline = processorPipeline;
79         }
80     }
81     
82     
83     /**
84      * Add a <code>Channel</code> to be processed by this
85      * <code>Selector</code>
86      */

87     public synchronized void addChannel(SocketChannel JavaDoc channel)
88             throws IOException JavaDoc, ClosedChannelException JavaDoc {
89         channels.add(channel);
90         selector.wakeup();
91     }
92
93
94     /**
95      * Register all <code>Channel</code> with an OP_READ opeation.
96      */

97     private synchronized void registerNewChannels() throws IOException JavaDoc {
98         int size = channels.size();
99         for (int i = 0; i < size; i++) {
100             SocketChannel JavaDoc sc = channels.get(i);
101             sc.configureBlocking(false);
102             try {
103                 SelectionKey JavaDoc readKey =
104                         sc.register(selector, SelectionKey.OP_READ);
105                 setSocketOptions(((SocketChannel JavaDoc)readKey.channel()).socket());
106             } catch (ClosedChannelException JavaDoc cce) {
107             }
108         }
109         channels.clear();
110     }
111
112     
113     /**
114      * Initialize this <code>SelectorThread</code>
115      */

116     public void initEndpoint() throws IOException JavaDoc, InstantiationException JavaDoc {
117         setName("SelectorReaderThread-" + port);
118         initAlgorithm();
119         initPipeline();
120         initProcessorTask(maxProcessorWorkerThreads);
121         initReadTask(minReadQueueLength);
122     }
123     
124     
125     /**
126      * Start and wait for incoming connection
127      */

128     public void startEndpoint() throws IOException JavaDoc, InstantiationException JavaDoc {
129         running = true;
130         
131         if (readPipeline != null){
132             readPipeline.startPipeline();
133         }
134
135         while (running) {
136             try{
137                 if ( selector == null ){
138                     selector = Selector.open();
139                 }
140                 
141                 registerNewChannels();
142                 doSelect();
143             } catch (Throwable JavaDoc t){
144                 logger.log(Level.FINE,"selectorThread.errorOnRequest", t);
145             }
146         }
147     }
148
149     
150     /**
151      * Provides the count of request threads that are currently
152      * being processed by the container
153      *
154      * @return Count of requests
155      */

156     public int getCurrentBusyProcessorThreads() {
157         return (processorPipeline.getCurrentThreadsBusy());
158     }
159     
160 }
161
Popular Tags