您的位置:首页 > 运维架构 > Tomcat

tomcat Connector 连接器

2014-12-30 19:09 218 查看


连接器的核心功能,本文去除非核心功能,留下整个程序的框架,便于理解。

1、接受连接请求

2.创建request,和response.

3.调用容器对应的Invoke方法,

首先看类的依赖结构。

1.Connetor构造方法,根据具体的协议名字,创建协议处理器,主要有NIO,BIO,AJP,协议。如果要自定义协议处理剂最重要的协议处理器了。如图,协议处理需要实现ProtocoHandler接口,

构造函数输入为协议名称

publicConnector(Stringprotocol){
setProtocol(protocol);
//Instantiateprotocolhandler
ProtocolHandlerp=null;
try{
Class<?>clazz=Class.forName(protocolHandlerClassName);
//实例化一个协议处理器
p=(ProtocolHandler)clazz.newInstance();
}catch(Exceptione){
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和相应此处不仔细深入。很简单。就是创建请求和相应对象

publicRequestcreateRequest(){

Requestrequest=newRequest();
request.setConnector(this);
return(request);

}

/**
*Create(orallocate)andreturnaResponseobjectsuitablefor
*receivingthecontentsofaResponsefromtheresponsibleContainer.
*/
publicResponsecreateResponse(){

Responseresponse=newResponse();
response.setConnector(this);
return(response);

}


3.启动和关闭。是默认方法;

protectedvoidstartInternal()throwsLifecycleException{

//Validatesettingsbeforestarting
if(getPort()<0){
thrownewLifecycleException(sm.getString(
"coyoteConnector.invalidPort",Integer.valueOf(getPort())));
}

setState(LifecycleState.STARTING);

try{
protocolHandler.start();
}catch(Exceptione){
StringerrPrefix="";
if(this.service!=null){
errPrefix+="service.getName():\""+this.service.getName()+"\";";
}

thrownewLifecycleException
(errPrefix+""+sm.getString
("coyoteConnector.protocolHandlerStartFailed"),e);
}
}

protectedvoidstopInternal()throwsLifecycleException{


setState(LifecycleState.STOPPING);


try{
protocolHandler.stop();
}catch(Exceptione){
thrownewLifecycleException
(sm.getString
("coyoteConnector.protocolHandlerStopFailed"),e);
}
}



我们从代码看到ProtocoHandler的重要性了明天再看
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: