Tomcat source code analysis (1): tomcat source code analysis
During this time, I briefly looked at the Tomcat source code and made a note here!
1. tomcat Architecture
Catalina: tomcat's top-level container. In the main () method, you can create a Catalina object instance to start or close tomcat;
Server: a container that manages all tomcat components, including one or more services;
Service: A Service is a collection of ctor and Container. The Service uses an appropriate Connector to receive user requests and then sends them to the corresponding Container for processing;
Connector: The main function is to receive ◇ socket ◇ handle socket ◇ encapsulate the corresponding request and response according to the protocol type and deliver it to the Container;
Container: The Engine Container receives requests from ctor and transmits them to the Pipeline of the sub-Container in sequence through Pipeline;
Engine: In the Valve invoke method in the Pipeline of the Engine, locate the next host based on request. getHost;
Host: A Web server virtual machine that manages specific web applications;
Context: the Context of a specific Web application. Each request is processed in a specific Context;
Wrapper: corresponds to each Servlet of the Web;
Next, we will mainly learn two major containers in tomcat: Connector and Container.
2. Connector container
The main problem solved by the Connector container is Socket reception. To better handle various protocols and concurrent asynchronous reception, protoctor has added two components: ProtocolHandler and EndPoint.
The main function of ProtocolHandler is to analyze the protocol header according to the definition of each protocol according to a certain format sentence and encapsulate it into request and response objects;
AbstractEndPoint is committed to receiving and processing sockets with high concurrency;
2.1 AbstractEndPoint
Two Runnable nodes in the EndPoint:
(1) the Accepter is responsible for receiving customer requests using ServerSocket. accept () and handing the Socket after the connection is established to Poller;
(2) Poller is responsible for receiving and processing requests;
protected class Acceptor extends AbstractEndpoint.Acceptor (Acceptor implements Runnable)public class Poller implements Runnable
Specific Writing of Accepter and Poller (For details, refer)
From this, we can find that after the Acceptor receives a user's socket request, it encapsulates this Socket into a PollerEvent and puts it into the events queue. Poller is actually always while (true). When there is a PollerEvent in the events queue, a Channel with data will be obtained from its Selector and handed over to ProcessKey () for processing.
2.1 ProtocolHandler
Each protocol has its own specific definition and format of the specific protocol header. After receiving the client request, we should adopt the corresponding resolution method according to the protocol type.
Role of ProtocolHandler:
◆ Define the AbstractEndPoint for specific Socket processing;
◆ Provide the AbstractConnectionHandler of the Resolution request to obtain the specific protocol header;
◆ Related init, start, and stop Methods
[References]
[1] http://blog.csdn.net/cutesource/article/details/5006062
[2] http://blog.csdn.net/yanlinwang/article/details/45648039