Tomcat startup process

Table of contents

One: Process

Two: source code analysis

1.Lifecycle

2. The default implementation of each component

3. Source code entry

Three: Summary


One: Process

 

steps :
1 ) To start tomcat , you need to call bin/startup.bat ( in the linux directory , you need to call bin/startup.sh), and in the startup.bat script , call catalina.bat .
2 ) In the catalina.bat script file, the main method in BootStrap is called .
3 ) The init method is called in the main method of BootStrap to create Catalina and initialize the class loader.
4 ) The load method is called in the main method of BootStrap , and the load method of Catalina is called in it .
5 ) In the load method of Catalina , some initialization work is required , and a Digester object needs to be constructed , using
for parsing XML .
6 ) Then call the initialization operation of subsequent components. . . Load the Tomcat configuration file, initialize the container component, listen to the corresponding port number, and prepare to accept client requests.

Two: source code analysis

1.Lifecycle

              Since all components have life cycle methods such as initialization, start, and stop, they have the characteristics of life cycle management.
Therefore, when Tomcat is designed, it abstracts an interface Lifecycle based on life cycle management , and the components Server , Service , Container , Executor , and Connector all implement a life cycle interface, thus having the following life cycle Core method:

1 ) init (): Initialize the component
2 ) start (): start the component
3 ) stop (): stop component
4 ) destroy (): Destroy the component

 

2. The default implementation of each component

The Server , Service , Engine , Host , and Context                we mentioned above are all interfaces, and the default implementation classes of these interfaces are listed in the figure below. Currently, for the Endpoint component, there is no corresponding Endpoint interface in Tomcat , but there is an abstract class AbstractEndpoint , under which there are three implementation classes: NioEndpoint , Nio2Endpoint, AprEndpoint , these three implementation classes correspond to the previously explained linker Coyote When mentioning the three IO models supported by the linker , NIO , NIO2 , APR , and Tomcat8.5 , the default is NioEndpoint .

ProtocolHandler : Coyote protocol interface, by encapsulating Endpoint and Processor , it realizes the processing function for specific protocols. Tomcat provides 6 implementation classes according to the protocol and IO .

AJP agreement:
1 ) AjpNioProtocol : adopts the IO model of NIO .
2 ) AjpNio2Protocol : adopts the IO model of NIO2 .
3 ) AjpAprProtocol : The IO model using APR needs to depend on the APR library.
HTTP protocol:
1 ) Http11NioProtocol : Adopt the IO model of NIO , the protocol used by default (if the server does not have APR installed).
2 ) Http11Nio2Protocol : adopts the IO model of NIO2 .
3 ) Http11AprProtocol : The IO model using APR needs to depend on the APR library.

3. Source code entry

Directory: org.apache.catalina.startup
MainClass BootStrap ‐‐‐‐> main(String[] args)

 

Three: Summary

           From the startup flowchart and the source code, we can see that the startup process of Tomcat is very standardized, and it starts uniformly according to the definition of the lifecycle management interface Lifecycle. First call the init() method to initialize the components step by step, and then call the start() method to start.
           In addition to completing its own processing, components at each level are also responsible for invoking the lifecycle management methods of subcomponent responses. Components are loosely coupled because we can easily modify and replace them through configuration files.

Guess you like

Origin blog.csdn.net/qq_61313896/article/details/128896104