Tomcat:组成


tomcat.png

Tomcat 的核心组件是 ConnectorContainer。多个 Connector 和一个 Container 组成 Service 提供服务。

代码参考7.0.93


1. Connector

Connector:监听特定端口号上的请求,创建 RequestResponse 对象用于处理请求数据和响应请求;每个请求都会产生一个对应线程来处理。

A particular instance of this component listens for connections on a specific TCP port number on the server. Each incoming request requires a thread for the duration of that request.

Connector 和 Container 的关系是一一对应的。

A Connector has one-to-one relationship with a Container.


2. Container

Container

Container 是处理 Connector 请求线程的组件。

A container is a module that processes the requests for a servlet and populates the response objects for web clients.

public interface Container extends Lifecycle {
    public static final String ADD_CHILD_EVENT = "addChild";
    @Deprecated
    public static final String ADD_MAPPER_EVENT = "addMapper";
    public static final String ADD_VALVE_EVENT = "addValve";
    public static final String REMOVE_CHILD_EVENT = "removeChild";
    @Deprecated
    public static final String REMOVE_MAPPER_EVENT = "removeMapper";
    public static final String REMOVE_VALVE_EVENT = "removeValve";
    public Manager getManager();
    public void setManager(Manager manager);
    @Deprecated
    public Object getMappingObject();
    public ObjectName getObjectName();
    public Pipeline getPipeline();
    public Container getParent();
    public void setParent(Container container);
    public ClassLoader getParentClassLoader();
    public void setParentClassLoader(ClassLoader parent);
    public Realm getRealm();
    public void setRealm(Realm realm);
    public DirContext getResources();
    public void setResources(DirContext resources);
    public void backgroundProcess();
    public void addChild(Container child);
    public void addContainerListener(ContainerListener listener);
    public void addPropertyChangeListener(PropertyChangeListener listener);
    public Container findChild(String name);
    public Container[] findChildren();
    public ContainerListener[] findContainerListeners();
    @Deprecated
    public void invoke(Request request, Response response)
        throws IOException, ServletException;
    public void removeChild(Container child);
    public void removeContainerListener(ContainerListener listener);
    public void removePropertyChangeListener(PropertyChangeListener listener);
    public void fireContainerEvent(String type, Object data);
}

有 4 种不同概念的 Container:

  • Engine:代表 servlet 引擎。

  • Host:代表有多个 Context 的虚拟主机。

  • Context:代表一个 web 应用。一个应用有一个或多个 Wrapper

  • Wrapper:代表一个独立的 servlet。

Container 可以有一个或多个低层级的 Container

A container can have zero or more child containers of the lower level.

Engine

代表处理一个请求流水线(request processing pipeline)的组件。Engine处理所有来自Connector的请求,并把响应返回给响应的Connector

An engine represents the entire Catalina servlet engine. If used, an engine is always the top level container. Child containers added to an engine are normally implementations of org.apache.catalina.Host or org.apache.catalina.Context.

Host

Host是一个虚拟主机(virtual host),代表联系网络名称(network name)和特定Tomcat服务器(server)的组件。Engine可以包含多个Host

客户端会把它希望访问的服务器放在HTTP header中(例如:www.mycompany.com)。Tomcat会拿到这个名称并查找名称匹配的Host

You use a host if you want to run more than one context in the same Tomcat deployment.

Context

Context 代表一个web应用(就是一个war包),它具备了 Servlet 运行的基本环境,理论上只要有 Context 就能运行 Servlet 了。简单的 Tomcat 可以没有 Engine 和 Host。

A context usually has one or more wrappers as its child containers.

context can read and parse the default web.xml file.

Wrapper

Since a wrapper is the lowest level of container, you must not add a child to it.

Wrapper 的重要方法:

public interface Wrapper extends Container {
    public Servlet allocate() throws ServletException;
    public void load() throws ServletException;
}
  • allocate():分配一个准备好调用 service() 方法的 servlet。
  • load():加载并初始化 servlet。

3. Pipeline 任务

A pipeline contains tasks that the container will invoke. A valve represents a specific task. There is one basic valve in a container’s pipeline, but you can add as many valves as you want.

The basic valve, which is invoked last, is responsible for processing the request and the corresponding response.

The basic valve is always called the last.

A container can have one pipeline.

PipeLine

public interface Pipeline {
    public Valve getBasic();
    public void setBasic(Valve valve);
    public void addValve(Valve valve);
    public Valve[] getValves();
    public void removeValve(Valve valve);
    public Valve getFirst();
    public boolean isAsyncSupported();
    public Container getContainer();
    public void setContainer(Container container);
}

Valve

public interface Valve {
    public String getInfo();
    public Valve getNext();
    public void setNext(Valve valve);
    public void backgroundProcess();
    public void invoke(Request request, Response response)
        throws IOException, ServletException;
    public void event(Request request, Response response, CometEvent event)
        throws IOException, ServletException;
    public boolean isAsyncSupported();
}

Contained

A valve class can optionally implement the org.apache.catalina.Contained interface. This interface specifies that the implementing class is associated with at most one container instance.

public interface Contained {
    public Container getContainer();
    public void setContainer(Container container);
}

4. Service & Server

Service

多个 Connector 和一个 Container 构成一个 Service

public interface Service extends Lifecycle {
    public Container getContainer();
    public void setContainer(Container container);
    public String getInfo();
    public String getName();
    public void setName(String name);
    public Server getServer();
    public void setServer(Server server);
    public ClassLoader getParentClassLoader();
    public void setParentClassLoader(ClassLoader parent);
    public void addConnector(Connector connector);
    public Connector[] findConnectors();
    public void removeConnector(Connector connector);
    public void addExecutor(Executor ex);
    public Executor[] findExecutors();
    public Executor getExecutor(String name);
    public void removeExecutor(Executor ex);
}

Server

Server 代表整个 servlet 容器,包括一切它的组件。

Server represents the entire Catalina servlet container and engulfs all other components. A server is particularly useful because it provides an elegant mechanism for starting and stopping the whole system. There is no need to start the connector and the container individually any longer.

Server 包含一个或多个 Service,并管理 Service 的生命周期。

Four methods are related to the lifecycle of the StandardServer: initialize, start, stop, and await.

Server 会一直监听一个端口(默认8005)获取关闭命令。

public interface Server extends Lifecycle {
    public String getInfo();
    public NamingResources getGlobalNamingResources();
    public void setGlobalNamingResources(NamingResources globalNamingResources);
    public javax.naming.Context getGlobalNamingContext();
    public int getPort();
    public void setPort(int port);
    public String getAddress();
    public void setAddress(String address);
    public String getShutdown();
    public void setShutdown(String shutdown);
    public ClassLoader getParentClassLoader();
    public void setParentClassLoader(ClassLoader parent);
    public Catalina getCatalina();
    public void setCatalina(Catalina catalina);
    public void addService(Service service);
    public void await();
    public Service findService(String name);
    public Service[] findServices();
    public void removeService(Service service);
}

5. Lifecycle


6. Mapper


参考

工作原理

Architecture Overview

The HTTP Connector


 Toc
 Tags