码迷,mamicode.com
首页 > 其他好文 > 详细

《how tomcat work》 搬运工 charpter 4:Default Connector

时间:2015-08-02 13:17:04      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

Default Connector是Tomcat 4用的connector 

Default Connector的工作是等待HTTP请求,创建request和response对象,然后传给container,然后container加载servlet来进行处理

 

 

Default Connector和charpter 3里的Connector区别是:

1、connector的HttpProcessor是同步的,要等到一个请求结束后才可以处理下一个请求。而Default Connector的HttpProcessor是在一个新的线程里进行的,也就是可以同时处理多个请求,而且HttpProcessor是从HttpProcessor poor里获取的

2、connecrot的HttpProcessor直接创建servlet来进行下一步处理,而Default Connector的HttpProcessor则通过Container来进行下一步处理。

 

 

Default Connector处理请求的流程:

1.等待请求

while (!stopped) { 
    Socket socket = null;
     try { 
            socket = serverSocket.accept(); 
     ...

2.获取HttpProcessor实例

       HttpProcessor processor = createProcessor();

3.assign socket

        processor.assign(socket);

 

 

HttpProcessor的处理流程

1.run是等待await()方法来获取socket,

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); 
    } 


// Tell threadStop() we have shut ourselves down successfully
    synchronized (threadSync) { 
        threadSync.notifyAll(); 
    } 
}

2.await()方法

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); 
}

3.assign()方法

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(); 
}

await()和assign()来靠bool available来判别是否已经存入socket

其中,wait()是将线程变成等待,而notifyAll()则是将所有线程(await())唤醒从而使await()返回socket

接下去就是container是事情了

 

《how tomcat work》 搬运工 charpter 4:Default Connector

标签:

原文地址:http://www.cnblogs.com/xuyung/p/4695431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!