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

[笔记]深入剖析Tomcat-tomcat的默认连接器,servlet容器

2012-12-31 15:27 701 查看
连接器

实现Connector接口

HttpConnector实例维护一个HttpProcessor实例池,个数有minProcessors和maxProcessors决定,如果HttpProcessor实例的数目达到max,则http请求则会被忽略。





public final class Bootstrap(){
public static void main(String[] args){
HttpConnector httpConnector = new HttpConnector();
SimpleContainer container = new SimpleContainer();
httpConnector.setContainer(container);
connector.initialize();
connector.start();
}
}
public class HttpConnector implements Connector, Runable{
protected int minProcessors = 5;
private int maxProcessors = 20;
private Stack processors = new Stack();

public void start{
while (curProcessors < minProcessors){
if (maxProcessors>0 && curProcessors >= maxProcessors)
break;
HttpProcessor httpProcessor = new HttpProcessor();
recycle(httpProcessor);
}
}

public void run(){
while(!loop){
//...省略
ServerSocket serverSocket = new(...)
Socket socket = setverSocket.accept();
HttpProcessor httpProcessor = createHttpProcessor(this); //从processor池中读取,如果无且小于max,则创建
if (httpProcessor==null) //processor池中无实例
socket.close();
httpProcessor.assign(socket);
}
}

public void recycle(HttpProcessor httpProcessor){
processors.push(httpProcessor);//入栈
}
}

public class SimpleContainer implements Container{
//...
public void invoke(Request request, Response response){
URL[] urls = new URL(1);
//...url 处理
String servletName = null;
//...servletName处理
URLClassLoader loader = new URLClassLoader(urls);
Class class = loader.loadClass(servletName);
Servlet servlet = class.newInstance();
servlet.service((HttpServletRequest)request, (HttpServletResponse)response);
}
}

public class HttpProcessor implements Runable{

public void run(){
while(!loop){
Socket socket = await();
if (socket == null){
continue;
}
//...
process(socket);
connector.recycle(this);
synchronized (threadSync){
threadSync.notifyAll();
}
}
}

private synchronized void assign(Socket socket){
while (available){
wait();
}
//...
this.socket = socket;
available = true;
notifyAll(); //唤醒await处理
}

private synchronized Socket await(){
while (!available){
wait();
}
Socket socket = this.scoket;
available = false;
notifyAll(); //告诉assign,处理完成,可以接收下一个
return socket;
}

public void process(Socket socket){

//parse request header parameters and so on..., response
//process, call container.invoke
}
}


servlet容器

context应用程序

public final class Bootstrap{
public static void main(String[] args){
HttpConnector httpConnector = new HttpConnector();
Wrapper wrapper1 = new SimpleWrapper();//实际处理servlet,context子容器
wrapper1.setName("wrapper1");
wrapper1.setServletClass("ClientServlet1");
Wrapper wrapper2 = new SimpleWrapper();
wrapper2.setName("wrapper2");
wrapper2.setServletClass("ClientServlet2");

Context context = new SimpleContext(); //与连接器相关联的主容器
context.addChild(wrapper1);
context.addChild(wrapper2);

Valve valve1 = new HeaderLoggerValve(); //与容器相关联
Valve valve2 = new ClientIPLoggerValve();

((Pipeline)context).addValve(valve1);
((Pipeline)context).addValve(valve2);

Mapper mapper = new SimpleContextMapper(); //映射器
mapper.setProtocol("http");

context.addMapper(mapper);

Loader loader = new SimpleLoader();
context.setLoader(loader);
context.addServletMapping("/Primitive", "wrapper1");
context.addServletMapping("/Moder", "wrapper2");

httpConnector.initialize();
httpConnector.start();
}
}

//     1.调用context的invoke
public class SimpleContext implements Context, Container{
// addServletMapping
//findServletMapping
//addMapper
//map();返回负责处理当前的wrapper实例
public void invoke(Request request, Response response){
pipeline.invoke(request, response); //
}
}

//      2.调用管道invoke方法
public class SimplePipeline {
public class SimplePipelineValveContext implements ValveContext{
//invokeNext
}
public void invoke(Request request, Response response){
(new SimplePipelineValveContext()).invokeNext(request, response); //会调用添加到context实例中的阀,调用基础阀SimpleContextValve的invoke方法,
}
}

//     4.调用基础阀invoke
public class SimpleContextValve implements valve{
public void invoke(Request request, Response response){
Wrapper wrapper = context.map(request, true); //使用context的映射器查找wrapper
wrapper.invoke(request, response);//再调用invoke方法
}
}

//    子容器基础阀invoke
public class SimpleWraperValve implements valve{
public void invoke(Request request, Response response){
Wrapper wrapper = context.map(request, true); //使用context的映射器查找wrapper
wrapper.invoke(request, response);//再调用invoke方法
}
}

//        5.调用子容器的invoke
public class SimpleWrapper implements Wrapper{
private SimplePipeline pipeline = new SimplePipeline();
pipeline.setBasic(new SimpleWraperValve);
public void invoke(Request request, Response response){
pipeline.invoke(request, response); //context的子容器,
}
}

public class SimpleContextMapper implements Mapper{
//...
public Container map(Request request, boolean update){
String contextPath = ((HttpRequest)request.getRequest().getContextPath();
String relativeURI= request.getDecodeRequestURI().subString(contextPath.length()); //get ServletPath
String name = context.findServletMapping(relativeURI);
return (Wrapper)context.findChild(name);
}
}

//     3.调用context容器 阀的invoke
public class ClientIPLoggerValve implements Valve, Contained{
//...
@Override
public void invoke(Request arg0, Response arg1, ValveContext valveContext) throws IOException,
ServletException {
valveContext.invokeNext(Request arg0, Response arg1);  //传递给管道中的下一个阀
// TODO Auto-generated method stub
}
}

public class HeaderLoggerValve implements Valve{
//...

@Override
public String getInfo() {
// TODO Auto-generated method stub
return null;
}

@Override
public Valve getNext() {
// TODO Auto-generated method stub
return null;
}

@Override
public void invoke(Request arg0, Response arg1) throws IOException,
ServletException {
// TODO Auto-generated method stub

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: