Tomcat source Analysis (a)------architecture

Source: Internet
Author: User
Tags event listener

First look at the server.xml of Tomcat.

<server port= "8005" shutdown= "shutdown" > <listener classname= " Org.apache.catalina.core.AprLifecycleListener "sslengine=" on "/> <listener classname=" Org.apache.catalina.core.JasperListener "/> <listener classname=" Org.apache.catalina.core.JreMemoryLeakPreventionListener "/> <listener classname=" Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener "/> <listener classname=" Org.apache.catalina.core.ThreadLocalLeakPreventionListener "/> <GlobalNamingResources> <resource name= "Userdatabase" auth= "Container"type= "Org.apache.catalina.UserDatabase"Description= "User database that can be updated and saved"Factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"Pathname= "Conf/tomcat-users.xml"/> </GlobalNamingResources> <service name= "Catalina" > <connector port= "80 "Protocol=" http/1.1 "ConnectionTimeout= "20000"Redirectport= "8443"/> <connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/> <engine name= "Catalina" Defa ulthost= "localhost" > <realm classname= "Org.apache.catalina.realm.LockOutRealm" > <realm classname= " Org.apache.catalina.realm.UserDatabaseRealm "resourcename= "Userdatabase"/> </Realm> Unpackwars= "true" autodeploy= "true" > <valve classname= "org.apache.catalina.valves.AccessLogValve" directory= "Logs"prefix= "Localhost_access_log." suffix= ". txt"pattern= "%h%l%u%t &quot;%r&quot; %s%b "/> </Host> </Engine> </Service></Server>

Overall architecture:

    • Component-Oriented Architecture
    • Based on JMX
    • Event Listening

1) component-oriented architecture

The tomcat code appears to be huge, but structurally clear and simple, it consists mainly of a bunch of components, such as servers, Service, connector, etc., and manages these components based on JMX, In addition, the components that implement the above interface implement the interface lifecycle that represent the lifetime, so that its components perform a fixed lifetime and extend through the event listener lifecycleevent during its entire lifetime. The core class diagram for Tomcat is as follows:

Catalina: The main class that interacts with the start/close shell script, so if you want to study the startup and shutdown process, start with this class.

Server: is a container for the entire Tomcat component that contains one or more service.

Service:service is a collection of connector and container, and the service receives the user's request with the appropriate connector, which is then sent to the appropriate container for processing.

Connector: A connector that implements a protocol, such as the default implementation of HTTP, HTTPS, and AJP protocols.

Container: A container that can be understood to handle a type of request, usually handled in a way that wraps the processor of the request into a valve object and puts it in a certain order into a pipe of type pipeline. Container has multiple seed types: Engine, Host, context, and wrapper, and these seed types container, in turn, are processed with different granularity requests. In addition, container includes some basic services such as loader, manager, and Realm.

The engine:engine contains the host and context, and the corresponding host is processed in the corresponding context when the request is received.

Host: Is the virtual host that we understand.

Context: It is the contexts of the specific Web applications that we are deployed in, and each request is processed in the context of the corresponding.

Wrapper:wrapper is for each servlet's container, each servlet has a corresponding Wrapper to manage.

You can see that the core components of the server, Service, Connector, Container, Engine, Host, context, and wrapper are scoped down by layer, and are included by layer.

Here are some of the basic components that are used by container:

Loader: is used by container to load all the required classes.

Manager: is used by container to manage the session pool.

Realm: It is used to handle authorization and authentication in security.

After analyzing the core class, and then looking at the Tomcat boot process, the sequence diagram for Tomcat starts is as follows:

As can be seen, the Tomcat boot is divided into the init and start two processes, the core components have implemented the lifecycle interface, all need to implement the Start method, so in the START process from the server began to call the sub-component of the starting process.

2) based on JMX

Tomcat registers the process for each component, manages it through registry, and Registry is based on JMX, so looking at the init and start processes of the component is actually the Start method that initializes the Mbean and triggers the Mbean, Will see a lot of shapes like:

Registry.getregistry (null, NULL). Invoke (Mbeans, "Init", false);

Registry.getregistry (null, NULL). Invoke (Mbeans, "start", false);

This code, in effect, is to manage the behavior and lifetime of various components through JMX.

3) Event Listener

Each component has a variety of behaviors during its lifetime, and these actions trigger the corresponding events, and Tomcat is designed to extend these behaviors by listening for these times. When you look at the init and start of a component, you see a number of the following:

Lifecycle.firelifecycleevent (after_start_event, null); Such code, which is the trigger for a certain type of event, if you want to add your own behavior, just register the corresponding type of event.

Original: http://blog.csdn.net/cutesource/article/details/5006062

Tomcat source Analysis (a)------architecture

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.