"High Performance server" Tomcat anatomy

Source: Internet
Author: User

Introduction

Tomcat is a popular servlet container, and it is necessary for developers to spend some time loving you to understand their internal structure when dealing with containers as a whole. This article will analyze its internal structure from a few aspects.

  1. Overall structure
  2. Connector
    1. Initialization process
    2. How to handle a request
  3. Container
    1. Session Management
    2. Design Patterns
    3. Context
    4. Wrapper
Overall structure

First, let's look at the overall structure of Tomcat first. As shown, the top level of the entire Tomcat container is the server object, with multiple service subordinates. Each service has its own connector and container combination [1]. Where the connector handles the network connection and passes the wrapped data to the container. The container is finally responsible for interpreting the servlet and returning the result. Let's take a closer look at the internal structure of each level.

?

Figure 2.1 Tomcat overall structure

?

Server: represents the entire Web container service, a server can contain multiple services, and maintains a service collection that provides interfaces externally [1]. The server implements the lifecycle interface, which manages subordinate components through the Start,stop function. Let's look at some typical methods, which appear not only in the server layer, but also frequently in their subordinate components, so it is necessary to do some research.

Add a service.

The main function of the Addxxx method is to connect the component itself to the upper component. Specifically, in server, the downlevel service sets its parent object to the current server. The next step is to determine if the entire system has been initialized, and if this method is called before initialization, you do not have to initialize the newly added service. Instead, you need to actively call its initialization function. Similarly, the startup method is the same.

Start method:

The primary task of the Start method is to start a subassembly subordinate to the component, and the component itself is not controlled as to how the subassembly manages the components that start the next layer.

Initialize Method:

As with the Start method, the main task of initialize is to initiate the initialization of the subordinate component.

Await method:

。。。

。。。

The primary role of server is to manage the startup, initialization, and shutdown of global components. However, as with many components, you need to enter a wait state to conserve the cup resources after the initial work is completed, so the await method is required. The await method of the server is to call the server's await method after the bootstrap program starts and initializes the server, leaving it in a wait state, waiting for the arrival of the Shutdown command [2].

The above functions are widely used in other parts of Tomcat, although the details are not the same, but the overall functionality is consistent and will not be described later.

Service:

The main function of a service is to manage the connectors and containers that belong to him, simply speaking, he is a combination of connectors and containers. Where a connector can have multiple, stored as an array, you need to reapply space when adding a new connector. The efficiency issue here needs to be discussed.

The container can only have one service, and the reason for this is that the separation system may encounter parts that need to be changed and the parts that are unchanged. Specifically, the connector is responsible for handling network connections, and network connections may need to be adapted to a variety of situations, such as HTTPS and Http,ssl and non-SSL. Therefore, different connectors may be required to handle different protocols. Instead, the container is responsible for interpreting the servlet, which is developed for specific specifications and does not change. The service also implements the lifecycle interface, which, like server, manages the components of the subordinate through the Start,stop method.

Connector

The main function of the connector is to parse the HTTP request and wrap the HTTP information in it into the request and response object, which is passed to the subsequent container processing. This information includes HTTP request headers, parameters, cookies, and so on. It is important to note that Tomcat Catalina Connector support http1.1 Long connection function, long connection is a new feature in http1.1, in the original 1.0 protocol browser after initiating the request can be disconnected from the server, the Web page pictures and other resources need to re-request, will cause unnecessary overhead. A long connection is when the server is connected to the browser, and the connection is maintained until the browser actively closes the connection, which avoids unnecessary connection overhead. The connector internally handles concurrent requests through a self-made thread pool, but by reading the connector source code we will find that he is a single background thread. So how does the connector handle concurrent requests?

Initialization

In fact, at initialization time, the connector first produces a server socket object, then starts its own thread, and then generates a Httpprocessor thread pool in which all the threads are in the suspended state. After initialization is complete, the connector blocks itself within its own run method, waiting for the connection to arrive.

Processing requests

When the connection arrives, the connector is only responsible for establishing the connection, and then passing the connection to one of the awakened Httpprocessor objects in the thread pool. The Httpprocessor object is responsible for the specific concurrency work [2]. Initialize generates a server socket:

The Start method mainly works:

Run is responsible for establishing the connection and assigning the processor processing thread:

Httpprocessor Method of Assign:

Httpprocessor the Run Method:

Httpprocessor's await method:

Processor controls the sleep and wake of the thread through a available variable, which simply means that the initial value of setting the available is false, and the call to await in the processor run method first suspends itself. When the connector calls processor's Assign method, processor obtains the socket object, resets available to true, and wakes up the thread. At this point, the await is invalidated and the process method is executed. There are two main things done in process, first, parsing the HTTP request and wrapping it. Second, pass the wrapped request to the container and wait for it to return.

Process of processing requests

Container

Simply put, the container's primary responsibility is to pass the request to the servlet and return the response. Is the overall structure of the container, divided into four layers, from the top to the Engine,host,context,wrapper. Similar to the structure of Tomcat as a whole, the management of the containers is also a typical chain-like structure. For example, the start function of the engine starts all the containers of its subordinates.

?

Hierarchy of containers

Let's look briefly at the functions represented at each level.

    • Engine: Represents the entire Catalina servlet engine
    • Host: Represents a virtual host.
    • Context: Represents a web App app
    • Wrapper: Represents a single servlet

?

Container design pattern (responsibility chain design mode)

Responsibility chain design pattern a simple understanding is that a request is passed to the next level by invoking the Invoke () method of the referencing object after the parent object has finished processing. The responsibility chain model in Tomcat is widely used. From engine to host to context to wrapper all through a chain of responsibility, the entire container structure is strung together like a chain.

?

?

In order to facilitate maintenance and expansion, the method calls between containers uniformly adopt the "pipe + valve" mode, which is the above-mentioned responsibility chain design pattern. Let's look at a specific example:

Standarengine:

Standardpipeline:

Standardpipelinevalvecontext:

We can see that standarengine will call the set of valves in its pipe pipeline Standardpipelinevalvecontext, The Standardpipelinevalvecontext, in turn, passes the valve that is requested in its collection. Finally Standardpipelinevalvecontext will pass the request to basic valve basic and pass the request to the lower container.

Session Management

Let's take a look at the session management function of Tomcat. In general, Tomcat manages the session object through the session manager, and the session manager is responsible for creating, updating, and destroying session objects [2]. The following code snippet shows that host is looking for a corresponding session by SessionID in a cookie.

The main methods of the session Manager are:

    • CreateSession ()-Create session instance
    • Getmaxinactiveinterval (), Setmaxinactiveinterval () – Gets or sets an expiry time in seconds
    • Add (), remove () – adds and removes session instances from the session pool

The default session is stored in the server's memory, and when the server is shut down or out of memory, the session needs to be stored on disk, which is the session persistence feature.

?

?

In Tomcat, the persistence operation specification of the session is specified through the store interface.

The store interface has two more important methods

    1. The Save () method is used to store the established session object in some persistent memory
    2. The load () method loads the session object into memory from memory based on the identifier price of the Session object

There are two important classes for implementing this interface, namely, Filestore, which is responsible for storing session objects in a file. The other is Jdbcstore, which is responsible for the session object being stored in the database through JDBC.

Context Container

Key features of the context

    • The context instance represents a specific Web application with a basic environment for the servlet to run. The most important function of the context is to manage the servlet instances. Contains one or more wrapper instances, each wrapper represents a specific servlet definition
    • Standarcontext has the URL mapping function, which is responsible for finding the URL corresponding to the wrapper container.
    • Standardcontext supports runtime overloading of files and has the ability to heat deploy.

Context of the URL Mapping

The context maps URLs and corresponding wrapper containers through a map, which is obtained by matching four rules in the Standarcontextmaper map method.

First parse to get the name of the app to match:

Rule one: Exact match rule

Rule two: prefix matching rules

Rule three: extension matching rules

Default matching rule

Context the overloaded mechanism

Overloading means that when a file is modified in the Web. xml or web-inf/classes directory, Tomcat re-scans the directory. Standardcontext defines the Reloadable property to indicate whether the application has overloaded functionality enabled [3]. TOMCAT4, the Standardcontext object uses another thread to check the timestamp of all classes and jar files in the Web-inf directory.

The Standarcontext Run method loads the Webapploader that implements the Runnable interface:

Webapploader scans the context directory in the main method and notifies the reload thread to reload the entire context if changes occur.

The overload takes two steps, the first step is to close all wrapper containers, and the new Wrappeer and the old Wrappeer start together.

?

4.6 Wrapper Container

Wrapper represents a servlet that manages a servlet, including the loading, initialization, execution, and resource recycling of the servlet. It is important to note that wrapper is the lowest-level container, which has no sub-containers underneath it. Standardwrapper loads the Servlet class when the servlet is requested for the first time. It is dynamically loaded with servlet[2].

Filters for Wrapper

Filter is another important mechanism of wrapper, Web developers can set up multiple filters, each filter can be loaded with multiple filter classes. By reading the configuration file information, wrapper loads the filter sequentially and passes the request. The service method of the servlet is invoked at the end of the filter.

?

Standardwrapper needs to hide most of its public methods from the servlet programmer. Therefore, the Standardwrapper class wraps itself as an instance of the façade class Standardwrapperfacade. Façade design patterns are primarily used in a large system where subsystems need to communicate with each other but not expose too much information to other systems.

?

Summary

Tomcat is a more complex servlet container, this article simply introduces some of the important components and their operating principles, for other internal components, such as the log system, ClassLoader, etc. are not involved. The Tomcat in this article is version 4, and the new Tomcat is more complex, but the main principle should be unchanged. As a web developer, it is necessary to take a closer look at the internal structure of the server.

"High Performance server" Tomcat anatomy

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.