Mercurial Hosting > luan
annotate src/org/eclipse/jetty/server/nio/SelectChannelConnector.java @ 1032:eca26899c4bc
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2016 03:19:50 -0600 |
parents | 39154cfa58e4 |
children |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.server.nio; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.net.InetSocketAddress; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.net.Socket; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.nio.channels.SelectionKey; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.nio.channels.ServerSocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.nio.channels.SocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import org.eclipse.jetty.io.AsyncEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import org.eclipse.jetty.io.EndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import org.eclipse.jetty.io.nio.AsyncConnection; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import org.eclipse.jetty.io.nio.SelectChannelEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.io.nio.SelectorManager; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.server.AsyncHttpConnection; |
907
bc4e299de953
remove AbstractNIOConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
906
diff
changeset
|
34 import org.eclipse.jetty.server.Connector; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import org.eclipse.jetty.server.Request; |
887
df84a1741687
make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
36 import org.eclipse.jetty.server.Server; |
1001 | 37 import org.eclipse.jetty.server.AbstractHttpConnection; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 /* ------------------------------------------------------------------------------- */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 * Selecting NIO connector. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 * This connector uses efficient NIO buffers with a non blocking threading model. Direct NIO buffers |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 * are used and threads are only allocated to connections with requests. Synchronization is used to |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 * simulate blocking for the servlet API, and any unflushed content at the end of request handling |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 * is written asynchronously. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
47 * </p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 * This connector is best used when there are a many connections that have idle periods. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 * </p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 * @org.apache.xbean.XBean element="nioConnector" description="Creates an NIO based socket connector" |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 */ |
907
bc4e299de953
remove AbstractNIOConnector
Franklin Schmidt <fschmidt@gmail.com>
parents:
906
diff
changeset
|
54 public class SelectChannelConnector extends Connector |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 { |
970
d82eb99e8df6
remove ConnectorSelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
968
diff
changeset
|
56 private final SelectorManager _manager = new SelectorManager(this); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 |
887
df84a1741687
make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
58 public SelectChannelConnector(Server server,int port) |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
59 { |
887
df84a1741687
make Connector reference to server explicit
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
60 super(server,port); |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
61 addBean(_manager,true); |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
62 } |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
63 |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
64 @Override |
906 | 65 public final void accept() throws IOException |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
66 { |
906 | 67 ServerSocketChannel server = _acceptChannel; |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
68 |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
69 if (server!=null && server.isOpen() && _manager.isStarted()) |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
70 { |
950 | 71 final SocketChannel channel = server.accept(); |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
72 channel.configureBlocking(false); |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
73 Socket socket = channel.socket(); |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
74 configure(socket); |
950 | 75 this.server.threadPool.execute(new Runnable(){public void run(){ |
76 _manager.register(channel); | |
77 }}); | |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
78 } |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
79 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
80 |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
81 @Override |
1001 | 82 public void customize(AbstractHttpConnection con) throws IOException |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
83 { |
1001 | 84 con._endp.setMaxIdleTime(_maxIdleTime); |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
85 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
86 |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
87 @Override |
909 | 88 protected synchronized void doStart() throws Exception |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
89 { |
909 | 90 if (_acceptChannel == null) |
91 { | |
92 // Create a new server socket | |
93 _acceptChannel = ServerSocketChannel.open(); | |
94 // Set to blocking mode | |
95 _acceptChannel.configureBlocking(true); | |
96 | |
97 // Bind the server socket to the local host and port | |
98 _acceptChannel.socket().setReuseAddress(true); | |
99 InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port); | |
100 _acceptChannel.bind(addr); | |
971 | 101 if( _acceptChannel.socket().getLocalPort() != port ) |
909 | 102 throw new IOException("Server channel not bound"); |
103 } | |
104 | |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
105 super.doStart(); |
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
106 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 |
970
d82eb99e8df6
remove ConnectorSelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
968
diff
changeset
|
108 public AsyncConnection newConnection(SocketChannel channel,AsyncEndPoint endpoint) |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
109 { |
972
5ee36654b383
simplify AbstractHttpConnection
Franklin Schmidt <fschmidt@gmail.com>
parents:
971
diff
changeset
|
110 return new AsyncHttpConnection(this,endpoint); |
864
e21ca9878a10
simplify ThreadPool use
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
111 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 } |