标签:blog http io os 使用 ar java for sp
事实上在第三章,就已经有了连接器的样子了,只是那仅仅是一个学习工具,在这一章我们会開始分析tomcat4里面的默认连接器。
事实上这部分内容是在书的前言部分,本来在这一系列读书笔记一開始的时候,我就得给朋友们聊聊这部分,但当时确实没怎么看懂,就仅仅能拖到如今了。
一个servlet容器(Container)能够相应若干个连接器(Connector)。
private Stack processors = new Stack(); private HttpProcessor createProcessor() { synchronized (processors) { if (processors.size() > 0) return ((HttpProcessor) processors.pop()); if ((maxProcessors > 0) && (curProcessors < maxProcessors)) { return (newProcessor()); } } else { if (maxProcessors < 0) { return (newProcessor()); } else { return (null); } } } }
public void run() { // Loop until we receive a shutdown command while (!stopped) { // Accept the next incoming connection from the server socket Socket socket = null; try { socket = serverSocket.accept(); ....
HttpProcessor processor = createProcessor(); //分线程 if (processor == null) { try { socket.close(); } catch (IOException e) { ; } continue; //话说这个continue是干什么的? } processor.assign(socket); //主线程
private HttpProcessor newProcessor() { // if (debug >= 2) // log("newProcessor: Creating new processor"); HttpProcessor processor = new HttpProcessor(this, curProcessors++); if (processor instanceof Lifecycle) { try { ((Lifecycle) processor).start(); } catch (LifecycleException e) { log("newProcessor", e); return (null); } } created.addElement(processor); return (processor); }
public void run() { // Process requests until we receive a shutdown signal while (!stopped) { // Wait for the next socket to be assigned Socket socket = await(); if (socket == null) continue; // Process the request from this socket try { process(socket); } catch (Throwable t) { log("process.invoke", t); } // Finish up this request connector.recycle(this); //将自己又一次push进stack里 } synchronized void assign(Socket socket) { // Wait for the Processor to get the previous Socket while (available) { try { wait(); } catch (InterruptedException e) { } } // Store the newly available Socket and notify our thread this.socket = socket; available = true; notifyAll(); if ((debug >= 1) && (socket != null)) log(" An incoming request is being assigned"); }
private synchronized Socket await() { // Wait for the Connector to provide a new Socket while (!available) { try { wait(); } catch (InterruptedException e) { } } // Notify the Connector that we have received this Socket Socket socket = this.socket; available = false; notifyAll(); if ((debug >= 1) && (socket != null)) log(" The incoming request has been awaited"); return (socket); }
/** * Is there a new socket available? */ private boolean available = false;
默认连接器中的request对象是org.apache.cataline.Request接口的实例,uml类图例如以下
uml类图例如以下
public void invoke(Request request, Response response) throws IOException, ServletException { String servletName = ( (HttpServletRequest) request).getRequestURI(); servletName = servletName.substring(servletName.lastIndexOf("/") + 1); URLClassLoader loader = null; try { URL[] urls = new URL[1]; URLStreamHandler streamHandler = null; File classPath = new File(WEB_ROOT); String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ; urls[0] = new URL(null, repository, streamHandler); loader = new URLClassLoader(urls); } catch (IOException e) { System.out.println(e.toString() ); } Class myClass = null; try { myClass = loader.loadClass(servletName); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println(e.toString()); } Servlet servlet = null; try { servlet = (Servlet) myClass.newInstance(); servlet.service((HttpServletRequest) request, (HttpServletResponse) response); } catch (Exception e) { System.out.println(e.toString()); } catch (Throwable e) { System.out.println(e.toString()); } }
public final class Bootstrap { public static void main(String[] args) { HttpConnector connector = new HttpConnector(); SimpleContainer container = new SimpleContainer(); connector.setContainer(container); try { connector.initialize(); connector.start(); //注意这里还仅仅是单纯的函数调用,眼下跟线程还没关系,还没run呢 // make the application wait until we press any key. System.in.read(); } catch (Exception e) { e.printStackTrace(); } } }
初始化部分包括了一下lifecircle的问题,不用太在意,以后我们会说。
下一节我们说说tomcat里面的命令模式
博客中图片均来自how tomcat works一书
how tomcat works 读书笔记四 tomcat的默认连接器
标签:blog http io os 使用 ar java for sp
原文地址:http://www.cnblogs.com/lcchuguo/p/4025005.html