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是事情了
时间: 2024-10-05 21:22:55