HTTP Connector. Implementations of this interface provide connectors for the HTTP protocol. A connector receives requests (normally from a socket) and calls the handle method of the Handler object. These operations are performed using threads from the ThreadPool set on the connector. When a connector is registered with an instance of Server, then the server will set itself as both the ThreadPool and the Handler. Note that a connector can be used without a Server if a thread pool and handler are directly provided.
前面已经提到两种重要的Connector
1.SocketConnector
Socket Connector. This connector implements a traditional blocking IO and threading model.This Connector should only be used if NIO is not available.
2.SelectChannelConnector
Selecting NIO connector. This connector uses efficient NIO buffers with a non blocking threading model. Direct NIO buffers are used and threads are only allocated to connections with requests. Synchronization is used to simulate blocking for the servlet API, and any unflushed content at the end of request handling is written asynchronously.
This connector is best used when there are a many connections that have idle periods.
jetty 如何处理请求
从Connector开始着手,SocketConnector两个重要的方法:
SocketConnector.open() 。打开socket_serverSocket= new ServerSocket()
public void run(){
synchronized(_connections)
{
_connections.add(this);
}
while (isStarted() && !isClosed())
{
if (_connection.isIdle())
{
if (getServer().getThreadPool().isLowOnThreads())
{
int lrmit = getLowResourceMaxIdleTime();
if (lrmit>=0 && _sotimeout!= lrmit)
{
_sotimeout=lrmit;
_socket.setSoTimeout(_sotimeout);
}
}
}
_connection.handle();
}
}
内部怎么又有一个_connection对象?这个 _connection是org.mortbay.jetty.HttpConnection的一个实例,在SocketConnector.Connection对象被new时内部创建。
Jetty org.mortbay.jetty.HttpConnection
A HttpConnection represents the connection of a HTTP client to the server and is created by an instance of a Connector. It's prime function is to associate Request and Response instances with a EndPoint.
A connection is also the prime mechanism used by jetty to recycle objects without pooling. The Request,Response, HttpParser, HttpGenerator and HttpFields instances are all recycled for the duration of a connection. Where appropriate, allocated buffers are also kept associated with the connection via the parser and/or generator.
_parser = new HttpParser(_connector,endpoint,new RequestHandler(),_connector.getHeaderBufferSize(),_connector.getRequestBufferSize());
_requestFields = new HttpFields();
_responseFields = new HttpFields();
_request = new Request(this);
_response = new Response(this);
_generator = new HttpGenerator(_connector,_endp,_connector.getHeaderBufferSize(),_connector.getResponseBufferSize());
这里的Request和Response就是jetty对Servlet API中定义的javax.servlet.HttpServletRequest实现。
"main" prio=10 tid=0x000000004054b800 nid=0xf9e at breakpoint[0x00007f078256d000]
java.lang.Thread.State: RUNNABLE
at org.mortbay.jetty.AbstractConnector.doStart(AbstractConnector.java:279)
at org.mortbay.jetty.bio.SocketConnector.doStart(SocketConnector.java:147)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
- locked <0x00000000e18c7b60> (a java.lang.Object)
at org.mortbay.jetty.Server.doStart(Server.java:235)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
- locked <0x00000000e187f2a0> (a java.lang.Object)
at mytest.MyJettyTester.main(MyJettyTester.java:28)
AbstractConnector.doStart()实现:
if (_threadPool==null)
_threadPool=_server.getThreadPool();
if (_threadPool!=_server.getThreadPool() && (_threadPool instanceof LifeCycle))
((LifeCycle)_threadPool).start();
// Start selector thread
synchronized(this){
_acceptorThread=new Thread[getAcceptors()];
for (int i=0;i<_acceptorThread.length;i++)
{
if (!_threadPool.dispatch(new Acceptor(i)))
{
Log.warn("insufficient maxThreads configured for {}",this);
break;
}
}
}
AbstractConnector.Acceptor.run() 的实现,最终调用accept:
while (isRunning() && getConnection()!=null){
accept(_acceptor);
}
可见也是通过threadPool调用的。
问题:serverSocket.setReuseAddress()的作用?
答案:google