Tomcat的HttpConnector/HttpProcesser(tomcat源码学习)

先启动进程HttpProcesser,等请求到达后加入socket对象并执行process().tomcat5.5以及后续版本已经没有看到HttpProcesser对象了
package com.xly;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpConnecter {

public static void main(String... args){
try {
Socket sk=null;
HttpConnecter hc=new HttpConnecter();
HttpProcesser hp=new HttpProcesser(hc,sk);
hp.start();
ServerSocket socketserver=new ServerSocket(8888,60000);
sk=socketserver.accept();
hp.assign(sk);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


package com.xly;

import java.net.Socket;

public class HttpProcesser extends Thread{
public boolean available = false;
public boolean stopped = false;
public Socket socket;
public HttpConnecter httpconnecter;

public HttpProcesser(HttpConnecter httpconnecter,Socket socket) {
this.available = false;
this.socket=socket;
this.httpconnecter=httpconnecter;
}

public void setAvailable(boolean available){
this.available=available;
}

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 ((socket != null))
System.out.println("The incoming request has been awaited");

return (socket);

}

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 ((socket != null))
System.out.println("An incoming request is being assigned");

}

public void process(Socket sockets)
{
System.out.println("process.run");
}

public void run() {

// Process requests until we receive a shutdown signal
while (!stopped) {
System.out.println("Thread Run");
// 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) {
System.out.print("process.invoke");
}

// Finish up this request
// connector.recycle(this);

}

// Tell threadStop() we have shut ourselves down successfully
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值