Tomcat 7 Connector 精读(1)

这个类图是本人截取的最重要的类的方法和属性。
其中ProtocalHandler是协议处理器,tomcat支持的协议以下方法可以看到。不同协议实现了不同的ProtocalHandler类。
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.Http11NioProtocol");
} else if ("AJP/1.3".equals(protocol)) {
setProtocolHandlerClassName
("org.apache.coyote.ajp.AjpNioProtocol");
} else if (protocol != null) {
setProtocolHandlerClassName(protocol);
}
}
}
ProtocalHandler是整个Connector类的核心。
初始化Connector的时候;根据协议名字创建处理器对象。
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);
}
}
首先是初始化协议处理器(去除了不太重要的代码)
protected void initInternal() throws LifecycleException {
super.initInternal();
// 初始化Adapter
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter);
// 每个协议处理器都有对应的适配器,适配器干啥的呢?
protocolHandler.init();
}
Connector的启动,实则是启动对应的协议处理器的启动,
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 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);
}
}
各位看管看到这里,其实看到连接器类需要做如下工作
(1)创建请求对象
/**
* Create (or allocate) and return a Request object suitable for
* specifying the contents of a Request to the responsible Container.
*/
public Request createRequest() { Request request = new Request();
request.setConnector(this);
return (request); }
(2)创建响应对象
/**
* 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)传给这两个对象给容器,简单而言,就是在创建好对象后,传递给那个适配器类就OK了。CoyoteAdapter类
Tomcat 7 Connector 精读(1)的更多相关文章
- Tomcat 7 Connector 精读(2) CoyoteAdapter
这个适配器类只讲2个方法,构造方法中我们看到一个适配器对象有自己关联的连接器类. 其中Service的重要任务就是讲客户端端请求交给容器. public void service(org.apache ...
- Tomcat 7 Connector 精读(2) 协议处理器 Http11Protocol(待续)
. Http11Protocol是阻塞式IO的实现,上图的几个方法是它的生命周期相关的方法.
- Chapter 4: Tomcat Default Connector
一.概述 第三章介绍的connector是一个很好的学习工具,但是我们还可以做的更多.这一章介绍的是Tomcat4默认的connector. 一个Tomcat的connector是一个独立的模块,能够 ...
- Tomcat 核心组件 Connector
Connector是Tomcat的连接器,其主要任务是负责处理浏览器发送过来的请求,并创建一个Request和Response的对象用于和浏览器交换数据,然后产生一个线程用于处理请求,Connecto ...
- 关于 tomcat nio connector, servlet 3.0 async, spring mvc async 的关系
tomcat 的 org.apache.coyote.http11.Http11NioProtocol Connector 是一个使用 Java NIO 实现的异步 accept 请求的 connec ...
- 内嵌Tomcat的Connector对象的静态代码块
在排查问题的过程中发现Connector对象有一个静态代码块: static { replacements.put("acceptCount", "backlog&quo ...
- [转]Loadrunner Error code 10053 & Tomcat 连接器(connector)优化
LoadRunner提示错误:Error : socket0 - Software caused connection abort. Error code : 10053. 在今天的测试过程中发现,s ...
- Tomcat HTTP connector和AJP connector
Tomcat服务器通过Connector连接器组件与客户程序建立连接,“连接器”表示接收请求并返回响应的端点.即Connector组件负责接收客户的请求,以及把Tomcat服务器的响应结果发送给客户. ...
- Tomcat connector元素常用配置(最大连接数等)
在tomcat的server.xml中有类似: <Connector port=" minSpareTHreads=" URIEncoding="gbk" ...
随机推荐
- BZOJ 1593: [Usaco2008 Feb]Hotel 旅馆
Description 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 &l ...
- PAT-乙级-1006. 换个格式输出整数 (15)
1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们用字母B来表示“百” ...
- ***CI异常记录到日志:CodeIgniter中设计一个全局exception hook
在CodeIgniter中,当发生异常时,经常要通知系统管理员,因此有必要在全局的高度上 捕捉异常,因此可以写一个hook, 比如在config目录的hook.php中,加入: $hook['pre_ ...
- http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html
http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html
- cocos2d-html5 onEnter init ctor构造函数 ----js特有特性(和c++有一点不一样)
ctor 构造函数, new 一个对象的时候调用-----coco2d-js , 默认ctor,为对象的构造函数,其它也可以默认其它函数为构造函数. 说白了就是: ctor构造函数 new 对象后自动 ...
- 思科模拟器软件教程---教你如何划分Vlan
方法/步骤 1.打开Cisco Packet Tracer,点击[交换机],选择第三个图标2960交换机,按住鼠标左键拖动到工作区.这里有很多类型的交换机,但是我们比较常用的是这个. 2.我们选择[终 ...
- Java比较器对数组,集合排序一
数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...
- 【一】 sched.h
第一个数据结构体是 task_struct ,这个数据结构被内核用来表示进程,包含其所有信息. 定义于文件 include/linux/sched.h 中,先看看其完整定义 struct task_s ...
- TOP命令详解
TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...
- linux0.11文件分析
在kernel包中有几个重要的文件夹和文件,他们各司其职,处理着有关内核的一些功能操作.其中文件夹有三个:blk_drv(块设备驱动),chr_drv(字符设备驱动),math(数学协处理器) 文件 ...