tomcat Connector 连接器
连接器的核心功能,本文去除非核心功能,留下整个程序的框架,便于理解。
1、接受连接请求
2.创建request,和response.
3.调用容器对应的Invoke方法,
首先看类的依赖结构。
1.Connetor 构造方法,根据具体的协议名字,创建协议处理器,主要有NIO,BIO,AJP,协议。如果要自定义协议处理剂最重要的协议处理器了。如图,协议处理需要实现ProtocoHandler接口,
构造函数 输入为协议名称
public Connector(String protocol) { setProtocol(protocol); // Instantiate protocol handler ProtocolHandler p = null; try { Class<?> clazz = Class.forName(protocolHandlerClassName);
// 实例化一个协议处理器 p = (ProtocolHandler) clazz.newInstance(); } catch (Exception e) { log.error(sm.getString( "coyoteConnector.protocolHandlerInstantiationFailed"), e); } finally { this.protocolHandler = p; } if (!Globals.STRICT_SERVLET_COMPLIANCE) { URIEncoding = "UTF-8"; URIEncodingLower = URIEncoding.toLowerCase(Locale.ENGLISH); } }
2.createRequest 和相应 此处不仔细深入。很简单。就是创建请求和相应对象
public Request createRequest() { Request request = new Request(); request.setConnector(this); return (request); } /** * Create (or allocate) and return a Response object suitable for * receiving the contents of a Response from the responsible Container. */ public Response createResponse() { Response response = new Response(); response.setConnector(this); return (response); }
3.启动和关闭。是默认方法;
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); } }
protected void stopInternal() throws LifecycleException {
setState(LifecycleState.STOPPING);
try {
protocolHandler.stop();
} catch (Exception e) {
throw new LifecycleException
(sm.getString
("coyoteConnector.protocolHandlerStopFailed"), e);
}
}
我们从代码看到ProtocoHandler的重要性了明天再看