Tomcat Source Analysis (i)------architecture.

Source: Internet
Author: User

Nonsense less say, pull code, import Eclipse Open Dry, concrete steps can refer to http://hi.baidu.com/hateeyes/blog/item/7f44942a20ad8f9d023bf66d.html

Let's talk about my understanding of Tomcat architecture.

Overall Architecture: component-oriented architecture based on JMX event listening

1) component-oriented architecture

The tomcat code looks large, but structurally clear and simple, consisting primarily of a bunch of components, such as servers, Service, connector, and so on, and managing these components based on JMX, In addition, the component that implements the interface above also implements the interface lifecycle representing the lifetime, so that its components perform a fixed lifetime and extend through the event listening lifecycleevent during its whole lifetime. The core class diagram for Tomcat looks like this:

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

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

Service:service is a collection of connector and container, the service uses the appropriate connector to receive the user's request, and then to the corresponding container to deal with.

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

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

The Engine:engine contains host and context, and the corresponding host is processed in the corresponding context after receiving the request.

Host: Is the virtual host that we understand.

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

Wrapper:wrapper is for each servlet 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 progressively descending by layer and are included on a per-layer basis.

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

Loader: is used by container to load a variety of required class.

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

Realm: is used to handle security authorization and authentication.

After analyzing the core classes, and then looking at the Tomcat boot process, the sequence diagram of the Tomcat boot is as follows:

As you can see from the figure above, Tomcat startup is divided into init and start two processes, the core components implement the Lifecycle interface, all need to implement the Start method, so in the START process is to start from the server layer called the child component start process.

2) based on JMX

Tomcat registers the process for each component, managed through registry, and Registry is based on JMX, so looking at the component's init and start processes is actually initializing the Mbean and triggering the Mbean's Start method, Will see a lot of shapes like:

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

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

Such code, which is actually the behavior and lifetime of managing various components through JMX.

3) Event Listening

Each component has a variety of behaviors in its lifetime, and these actions trigger the corresponding event, and Tomcat is listening to these times to extend the behavior. See a lot of the init and start processes for components like this:

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

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.