Connector主要接收请求并将请求封装成Request和Response来具体处理
最底层是使用Socket来进行连接的
而Request和Response是HTTP协议来封装的,封装完之后交给Container来处理。
因此Container是Servlet的容器。
Container处理完之后返回给Connector,Connector通过使用Socket将结果返回给客户端整个请求就完成了。
上一回合分析到了Service的load 和start是调用了Connector的init load和start
既然是处理浏览器请求,那么需要支持http协议,在Tomcat中有两种协议处理器:HTTP/1.1与AJP/1.3协议处理器。在server.xml中已经指明tomcat所支持的两种协议:
在配置文件中指定了
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
只分析http1.1
分析Connector
@Override
protected void initInternal() throws LifecycleException {
super.initInternal();
// Initialize adapter
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter);
// Make sure parseBodyMethodsSet has a default
if( null == parseBodyMethodsSet ) {
setParseBodyMethods(getParseBodyMethods());
}
if (protocolHandler.isAprRequired() &&
!AprLifecycleListener.isAprAvailable()) {
throw new LifecycleException(
sm.getString("coyoteConnector.protocolHandlerNoApr",
getProtocolHandlerClassName()));
}
try {
protocolHandler.init();
} catch (Exception e) {
throw new LifecycleException
(sm.getString
("coyoteConnector.protocolHandlerInitializationFailed"), e);
}
// Initialize mapper listener
mapperListener.init();
}
另一个方法
/**
* Begin processing requests via this Connector.
*
* @exception LifecycleException if a fatal startup error occurs
*/
@Override
protected void startInternal() throws LifecycleException {
// Validate settings before starting
if (getPort() < 0) {
throw new LifecycleException(sm.getString(
"coyoteConnector.invalidPort", Integer.valueOf(getPort())));
}
setState(LifecycleState.STARTING);
try {
protocolHandler.start();
} catch (Exception e) {
String errPrefix = "";
if(this.service != null) {
errPrefix += "service.getName(): \"" + this.service.getName() + "\"; ";
}
throw new LifecycleException
(errPrefix + " " + sm.getString
("coyoteConnector.protocolHandlerStartFailed"), e);
}
mapperListener.start();
}
在代码中的体现没展示出来
/**
* Set the Coyote protocol which will be used by the connector.
*
* @param protocol The Coyote protocol name
*/
public void setProtocol(String protocol) {
if (AprLifecycleListener.isAprAvailable()) {
if ("HTTP/1.1".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.http11.Http11AprProtocol");
} else if ("AJP/1.3".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.ajp.AjpAprProtocol");
} else if (protocol != null) {
setProtocolHandlerClassName(protocol);
} else {
setProtocolHandlerClassName
("org.apache.coyote.http11.Http11AprProtocol");
}
} else {
if ("HTTP/1.1".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.http11.Http11Protocol");
} else if ("AJP/1.3".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.ajp.AjpProtocol");
} else if (protocol != null) {
setProtocolHandlerClassName(protocol);
}
}
}
Connector具体是用ProtocolHandler来处理请求,可以有多种Handler.
public interface ProtocolHandler {
/**
* The adapter, used to call the connector.
*/
public void setAdapter(Adapter adapter);
public Adapter getAdapter();
/**
* The executor, provide access to the underlying thread pool.
*/
public Executor getExecutor();
/**
* Initialise the protocol.
*/
public void init() throws Exception;
/**
* Start the protocol.
*/
public void start() throws Exception;
/**
* Pause the protocol (optional).
*/
public void pause() throws Exception;
/**
* Resume the protocol (optional).
*/
public void resume() throws Exception;
/**
* Stop the protocol.
*/
public void stop() throws Exception;
/**
* Destroy the protocol (optional).
*/
public void destroy() throws Exception;
/**
* Requires APR/native library
*/
public boolean isAprRequired();
}
不同的实现有
/**
* Set the Coyote protocol which will be used by the connector.
*
* @param protocol The Coyote protocol name
*/
public void setProtocol(String protocol) {
if (AprLifecycleListener.isAprAvailable()) {
if ("HTTP/1.1".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.http11.Http11AprProtocol");
} else if ("AJP/1.3".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.ajp.AjpAprProtocol");
} else if (protocol != null) {
setProtocolHandlerClassName(protocol);
} else {
setProtocolHandlerClassName
("org.apache.coyote.http11.Http11AprProtocol");
}
} else {
if ("HTTP/1.1".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.http11.Http11Protocol");
} else if ("AJP/1.3".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.ajp.AjpProtocol");
} else if (protocol != null) {
setProtocolHandlerClassName(protocol);
}
}
}
当然Connector有默认的ProtectHandler
/**
* Coyote Protocol handler class name.
* Defaults to the Coyote HTTP/1.1 protocolHandler.
*/
protected String protocolHandlerClassName =
"org.apache.coyote.http11.Http11Protocol";
它的构造函数
// ------------------------------------------------------------ Constructor
public Http11Protocol() {
endpoint = new JIoEndpoint();
cHandler = new Http11ConnectionHandler(this);
((JIoEndpoint) endpoint).setHandler(cHandler);
setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);
setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
}
而ProtocolHandler有3个重要组件 EndPoint,Processor,Adapter
- EndPoint用于处理底层网络连接 实现了TCP/IP协议
- Processor实现HTTP协议
- Adapter将请求适配到Servler容器具体处理。
Endpoint中有Acceptor和AsyncTimeout和SocketProcessor内部类和一个Handler接口。
Aceeptor用于监听请求,
AsyncTimeout用于异步检查request的超时
Handler用于处理接收到的Socket,在内部调用了Processor处理。
整个流程理完了 但具体还是得源码验证。。
这部分明天再写吧
Connector自身类
ProtocolHandler类
关于他的组件
EndPoint
Processor
Adapter
至此Tomcat已大概分析完了
然后下一次我会做一个整理总结
还会对之前分析不足的地方做继续的修正更新