Tomcat source Analysis (RPM)

Source: Internet
Author: User

Tomcat Source Analysis (a)--service startup

1. Tomcat has two components, connectors and containers, the so-called connector is an HTTP request come over, the connector is responsible for receiving this request, and then forwarded to the container. the container is the servlet container, and the container has many layers, namely the engine,

Host,context,wrapper. The largest container engine, representing a servlet engine, followed by host, representing a virtual machine, then the context, representing an application, wrapper corresponding to a servlet. From the connector

once connected, the container passes through the container in order and finally arrives at the specific servlet. to illustrate the engine,host two kinds of containers are not required. In fact, a simple tomcat can be as long as the connector and the container,

But the implementation of Tomcat in order to unify the management of components such as connectors and containers, add additional server components (server) and service components (services), add these two things I personally think that is to facilitate the unified management of the connector and

Various components such as containers. A server can have multiple service, a service contains multiple connectors and a container, and of course there are other things that make it easy to understand Tomcat's architecture by looking at the diagram below:

2. A parent component can also contain multiple subcomponents, and these managed components implement the lifecycle interface. as soon as a component is started, all of his subcomponents will follow, such as a server boot, all of its child

The service is started, the service starts, and all of its connectors and containers are started, so that Tomcat starts, as long as the server is started, and the other components follow the boot

3. Generally starting Tomcat will be run Startup.bat or startup.sh file, actually these two files will finally call the Org.apache.catalina.startup.Bootstrap class's Main method, this main method mainly do two things,

1: Define and initialize Tomcat's own ClassLoader, 2: Call the Process method of Org.apache.catalina.startup.Catalina by reflection;

4. The function of the process method is also very simple, 1: If the Catalina.home and catalina.base two properties are not set to set, 2: The argument is correct, call the Execute method, execute method is simply call the Start method,

Where the correct parameters of the method arguments set the starting ID to true, so that in the Execute method can call the Start method, the Start method is the focus, in it launched all of our Tomcat services

5. the most important method here is Createstartdigester (), and ((Lifecycle) server). Start (); The main function of the Createstartdigester method is to help us instantiate all the service components including Server,service and connect, as

How to instantiate the next look, the Start method is to start the service instance. File file = ConfigFile (); Is the new Server.xml file instance, and the service components are to be based on this file .

6. Digester is a class inside an external jar package, and the main function is to parse the elements inside the XML and generate the objects, set the attributes of the elements into the attributes of the objects, and form the relationship between the parent and the Child Brothers.

Digester.addobjectcreate ("Server", "Org.apache.catalina.core.StandardServer", "className");// Create a Org.apache.catalina.core.StandardServer object, and there is actually no real

Create an object, but add a pattern, but the object created later is based on these patterns and server.xml, so you can understand for the time being. The object that is actually created is the Digester.parse (IS) inside the Start method, which is

Server.xml file stream, Digester has just added service components such as Standardserver and Standardservice, as well as standardserver and standardservice relationships and Standardservice and Connector

HttpConnector, the container standardhost the relationship, so the Digester.parse (is) method is called and the object is generated based on the schema and the Server.xml file and the relationship between them. So we have the server component.

Standardserver object, also has its subcomponents Standardservice objects, and so on.

7. Now that you have an object for the server component, initialize and then start it, Tomcat implements the boot server component Standardserver. After the start of things to do more things, but it is relatively clear,

the key code for the Start method of Standardserver is to start its subcomponent standardservice. Standardservice's Start method is similar to the Standardserver start method, which is the connector and container that initiates it, which says a

Service contains one container and multiple connectors

8. The default connector is HttpConnector, so the start method of the HttpConnector is called. Here's a two key class: HttpConnector and Httpprocessor, which all implement the Runnable interface, HttpConnector

Responsible for receiving HTTP requests, Httpprocessor is responsible for processing requests received by HttpConnector. Note that there are many instances of httpprocessor here, the maximum can be maxprocessor, initialization is 20. So in

A background thread is started in the ThreadStart method to receive HTTP connections

9. In this way, the HttpConnector background thread is started, and its run method keeps looping, mainly creating a new serversocket to listen to the port waiting for the connection. ServerSocket has been waiting to connect and get connected to Httpprocessor

Example processor to deal with, ServerSocket continue to cycle monitoring, as to processor specific how to deal with, there is a lot to say, here first not say.

Tomcat source Analysis (ii)--Connection processing

1. A HttpConnector thread has been started in the previous section, and a fixed number of httpprocessor threads have been started. HttpConnector is used to wait for an HTTP connection, and an HTTP connection is given to one of the Httpprocessor

threads to handle. take a look at how HttpConnector get connected, and how httpprocessor is handled

2. The key here is Socket = serversocket.accept (), and processor.assign (socket), inside the Loop, serversocket.accept (); Responsible for receiving the HTTP request and assigning it to the socket, and finally to one of the processor

processing. Here processor is not waiting for the time needed to instantiate, but in the HttpConnector initialization has been a number of processor. HttpConnector holds a stack containing Httpprocessor objects, which is required

Time to take it out.

3. Next by processor.assign (socket); Remember that this method is asynchronous and does not need to wait for httpprocessor to finish, So httpconnector can make uninterrupted incoming HTTP requests.

4. Obviously, at the beginning of its run method, the await method is called to wait (because the first available variable is false), so the httpprocessor will block until the thread wakes it up. When called from the HttpConnector

Processor.assign (socket) will pass the socket to this Httpprocessor object and set available to True to invoke Notifyall () to wake the processor thread to process the socket. Meanwhile, in the await method, the available

is set to False, so it returns to its original state, which allows the socket to be re-accepted. the method of processing the socket here is the process (socket), the main function is two points, 1: Parsing the socket, that is, parsing the HTTP request, including the request method, request protocol, etc., to

Populate the Request,response object (not very familiar, the Request,response object that is often used in servlet and JSP development is from here). 2: Pass the Request,response object to the HttpConnector bound container and let the container

call the Invoke method for processing.

5. In those Parsexx methods will initialize the Request,response object, and then call the container's Invoke method to handle, at this point, the HTTP request over the connection has been perfectly transferred to the container processing, the remaining problem of the container is to eventually turn

the question of which servlet or JSP to give. as we know before, a connection is connected to a container, a container with a large level will have one or more sub-containers, the smallest container is wrapper, corresponding to a servlet, where we just need to know the requested

The path determines which Wrapper,wrapper will eventually be chosen to invoke the servlet. At least the questions raised at the outset have been understood.

Tomcat source Analysis (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.