How Tomcat works-II, Tomcat launcher (1)

Source: Internet
Author: User

Mainly introduces some of the interfaces and classes involved in Tomcat startup.

Directory
    • Overview
    • The components that Tomcat contains
    • Lifecycle
    • Container
    • Connector
    • Summarize
Overview

Tomcat as a server, its main function is to receive requests-processing requests-return, if we implement a simple server, start a thread to listen to a port, the port has data in the word to receive data, and then start a thread to process the received data and return. So starting a server is the most important thing is to start a thread to listen on a port, Tomcat is, but Tomcat contains many components, first to complete the initialization of the component, and then start the individual components in turn. So to know the boot process of Tomcat, first understand the components of Tomcat.

The components that Tomcat contains
    • Lifecycle: Manages the life cycle of the entire tomcat, including Start,stop. All Tomcat components implement the interface (or the class that implements the interface)
    • The most important part of the Container:tomcat, the servlet container
    • Connector:tomcat connector that listens for ports, receives requests, and distributes requests

In fact, including processor, Session, Manager, deployer, etc., but during the start-up process, these are not the most important, after understanding the whole process in the specific learning.

Lifecycle

The life cycle component consists of an interface, a class, a enum:lifecycle, a lifecycleevent, a lifecyclestate.

Lifecycle interface

The interface org.apache.catalina.Lifecycle contains the following definitions:

    • Basic methods for managing the life cycle, such as: Start,stop
    • For the management of Component listener
    • Events that may be triggered during the lifetime of a component, such as start_event,stop_event, for each constant string corresponding to a lifecycleevent

Abstract class Lifecyclebase implements the lifecycle interface, mainly provides the basic start and stop implementations, and adds the following methods

    • About the event: firelifecycleevent (protect, which can be overloaded and used by the quilt Class), to trigger all listener of the component
    • About initializing subcomponents: initinternal (Protect), startinternal (protect), etc., because many of the components in Tomcat are parent-child relationships (such as engine and host), which is also The elegant place for Tomcat lifecycle Management : Each component is responsible for starting its own subcomponents or related components (listening to some of the component's event start_event, etc.), so Tomcat just starts the topmost component

Abstract class Lifecyclembeanbase inherits the Lifecyclebase class, which implements some Lifecyclebase methods, provides some basic management of Mbean, and Tomcat provides support for JMX.

Lifecycleevent class

Org.apache.catalina.LifecycleEvent inherits from Java.util.EventObject, which represents lifecycle triggered events, and the events are closely related to the listener, All classes that want to perceive changes in the life cycle state of a component have to listen to this interface (Org.apache.catalina.LifecycleListener) in advance for the corresponding event. In fact, for example, the initialization of contextconfig is to listen to the context event.

Lifecycleenum

Provides a variety of states for lifecycle changes, reference (Tomcat Javadoc)

Container

In fact, the main part of Tomcat as a servlet container is the container component, the container component is a four-tier architecture with the following class diagram:

A simple introduction to the container's four-tier architecture:

    • Engine: Not necessary in the Catalina container architecture, but if you want to use the top level of the container, there is one or more hosts (virtual host), the engine is responsible for distributing the request to the corresponding virtual host
    • Host: Represents a virtual host with one or more context,host responsible for distributing the request to the corresponding context
    • Context: Represents a webapp with one or more wrapper, responsible for mapping the request to the corresponding wrapper
    • Wrapper: Represents a servlet that is responsible for loading and initializing the servlet
Connector

In the beginning we said that the implementation of the server is to listen to a port, receive requests, which is the role of Tomcat connector components, the last listening port is jioendpoint by an internal class acceptor to complete, so when jioendpoint$ When the acceptor thread starts, the entire Tomcat server starts to complete.

Tomcat supports the HTTP and AJP protocols, so Http11protocol and Ajpprotocol are provided to handle the corresponding protocol (which mainly draws the part of the HTTP protocol).

Summarize

The above are the main classes and interfaces that are involved in Tomcat startup, of course, which involves much more than this, but at the outset I also said that for different purposes, can have targeted learning, so the above only focus on some of the main class. Another problem is that I am still a bit tangled: in fact, I think the record is not necessary to add a large section of code, because the code is relatively understood, and if you really want to read the words can be found at any time, of course, for some very important or classic code is necessary to specifically analyze.

In the process of reading some notes and draw some pictures (these at the end of the Tomcat source reading is attached), but the feeling is more trivial, after such a finishing feeling more clear.

How Tomcat works-II, Tomcat launcher (1)

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.