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
	}
	

}

你可能感兴趣的:(connector)