[笔记]深入剖析Tomcat-tomcat的默认连接器,servlet容器
2012-12-31 15:27
701 查看
连接器
实现Connector接口
HttpConnector实例维护一个HttpProcessor实例池,个数有minProcessors和maxProcessors决定,如果HttpProcessor实例的数目达到max,则http请求则会被忽略。
![](http://images.cnitblog.com/blog/308840/201212/31105843-6a64a04c972c4165a2dd04736f969031.png)
![](http://images.cnitblog.com/blog/308840/201212/31105940-8e91abd401514a0797e1946d1d3a5bbc.png)
servlet容器
context应用程序
实现Connector接口
HttpConnector实例维护一个HttpProcessor实例池,个数有minProcessors和maxProcessors决定,如果HttpProcessor实例的数目达到max,则http请求则会被忽略。
![](http://images.cnitblog.com/blog/308840/201212/31105843-6a64a04c972c4165a2dd04736f969031.png)
![](http://images.cnitblog.com/blog/308840/201212/31105940-8e91abd401514a0797e1946d1d3a5bbc.png)
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 } }
相关文章推荐
- [笔记]深入剖析Tomcat-servlet容器和连接器
- 【深入剖析Tomcat笔记】第四篇 默认连接器
- 【深入剖析Tomcat笔记】第三篇 基本容器模型
- 深入剖析Tomcat-第二章:一个简单的servlet容器(1)
- 深入剖析tomcat之servlet容器
- Servlet容器原型(三)——Tomcat 4默认连接器浅谈
- 深入剖析Tomcat 第五章 Servlet容器
- 深入剖析Tomcat-一个简单的servlet容器
- 深入剖析tomcat之一个简单的servlet容器
- 深入剖析Tomcat-第二章:一个简单的servlet容器(2)
- [深入剖析Tomcat]一个简单的servlet容器实现
- 深入剖析Tomcat 第四章 默认连接器(续-处理器)
- 深入剖析tomcat之servlet容器
- [深入剖析Tomcat]一个简单的servlet容器实现2
- 深入剖析Tomcat 第四章 默认连接器
- 【深入剖析Tomcat笔记】第六篇 Tomcat Lifecycle
- 【深入剖析Tomcat笔记】第五篇 Tomcat Container 与 Pipelining Tasks
- [笔记]深入剖析Tomcat-生命周期,日志记录器
- 深入学习Tomcat——servlet容器
- HowTomcatWork 笔记 1 Servlet 容器做的3件事情