Tomcat设计原理个人学习理解

在看How Tomcat Works,收获颇深,结合最近看的代码和个人理解写些个人心得

1、Tomcat对每个request的管理

在org.apache.catalina.connector.http包下,有个HttpConnector连接器类。

它的工作过程是,先打开SocketServer端口,设置HttpProcessors的List大小,用于限制连接者数量。

当有一个http请求时,HttpConnector将请求都的socket交给HttpProcessor,由它负责创建HttpRequest和HttpResponse。由HttpResponse和HttpRequest对象来处理请求和返回响应请示结果。

2、Tomcat对多线程管理

HttpConnector类有的工作是监听请求。当有请求时,将请求交给HttpProcessor。因HttpProcessor继承Runnable接口,而HttpConnector类中可设置HttpProcessor堆栈数量。通过HttpProcessor来作多线程处理。

3、Tomcat对事件监听的处理方式

tomcat在很多地方通过观察者模式来实现消息订阅,请看以下代码:

 

 

//对象状态对象,做为参数传给订阅者

public final class LifecycleEvent

    extends EventObject {

 

    public LifecycleEvent(Lifecycle lifecycle, String type) {

 

        this(lifecycle, type, null);

 

    }

    public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {

 

        super(lifecycle);

        this.lifecycle = lifecycle;

        this.type = type;

        this.data = data;

 

    }

}

 

 

//生命周期订阅者,根据对象状态做相应处理

public interface LifecycleListener {

 

    public void lifecycleEvent(LifecycleEvent event);

 

}

 

 

//消息

public interface Lifecycle {

 

    public static final String START_EVENT = "start";

public static final String BEFORE_START_EVENT = "before_start";

……………………………………………

    public void addLifecycleListener(LifecycleListener listener);

public void removeLifecycleListener(LifecycleListener listener);

public LifecycleListener[] findLifecycleListeners();

 

public void start() throws LifecycleException;

public void stop() throws LifecycleException;

 

}

 

4、Tomcat分布式部署

 

5、Tomcat异常处理机制

tomcat异常处理对多国语言的支持,Tomcat在Util中有个类“StringManager”,通过调用getString方法到获得异常信息多国语言支持。

猜你喜欢

转载自wang-cheng.iteye.com/blog/1511099
今日推荐