Source:Github.com/c-rainstorm…

The title is a big one, and I was very upset when I wrote it, but I tried to describe the process as clearly as possible. Because this is a summary written after reading the source code, some preconditions may be ignored in the process of writing. If there is something unexpected or difficult to understand, please give me an Issue, AND I will supplement and revise the relevant content as soon as possible.

A lot of things are already very clear in sequence diagrams and there is no need to explain them step by step, so this article focuses on diagrams and briefly explains some of them.

Use PlantUML + Visual Studio Code + PlantUML Extension to draw the graphics

This article introduces Tomcat using tomcat-9.0.0.m22 as the standard.

Tomcat.apache.org/tomcat-9.0-…

Overview

  1. Bootstrap is Tomcat’s Bootstrap class for external startup. In the $CATALINA_BASE/bin directory, Bootstrap creates, initializes, and starts Catalina instances through reflection.
  2. Catalina parses the $CATALINA_BASE/conf/server. XML file and creates StandardServer, StandardService, StandardEngine, StandardHost, etc
  3. StandardServer represents the entire Servlet container, which contains one or more StandardServices
  4. StandardService contains one or more connectors and an Engine, both of which are created when parsing the conf/server.xml file, The standard implementation of Engine in Tomcat is StandardEngine
  5. MapperListener implements the LifecycleListener and ContainerListener interfaces to listen for container and lifecycle events. This listener instance listens to all containers, including StandardEngine, StandardHost, StandardContext, StandardWrapper, and registers the container with Mapper when the container changes.
  6. Mapper maintains the MAPPING between URLS and containers. When a request comes in, the Mapper determines which Host, Context, or Wrapper to map the request to based on the mapping information.
  7. Http11NioProtocol is used to handle HTTP/1.1 requests
  8. The NioEndpoint is the endpoint of the connection, which is the core class in the request processing process and is highlighted.
  9. The CoyoteAdapter is used to pass requests from Connctor to the Container for processing. Decouple Connctor from Container.
  10. StandardEngine represents a Servlet engine for processing requests accepted by the Connector. Contains one or more hosts (virtual hosts), and the standard implementation of Host is StandardHost.
  11. StandardHost represents a virtual host for deploying applications on that virtual host. It usually contains multiple contexts (Context stands for application in Tomcat). The standard implementation of Context in Tomcat is StandardContext.
  12. StandardContext represents a single application, usually containing multiple wrappers, a Wrapper container that encapsulates a Servlet, and the standard implementation of wrappers is StandardWrapper.
  13. The StandardPipeline component represents a pipeline, combined with valves (valves) to process requests. StandardPipeline has multiple valves, and when a Request needs to be processed, Valve’s invoke method is called one by one to process Request and Response. In particular, there is a special Valve called basicValve, and every standard container has a designated basicValve, which does the most core work.
  • StandardEngine is StandardEngineValve, which maps requests to specified hosts;
  • StandardHost is StandardHostValve, which maps Request to the specified Context;
  • StandardContext is the StandardContextValve, which maps Request to the specified Wrapper;
  • StandardWrapper is the StandardWrapper valve, which loads the Servlet specified by Rquest and calls the Servlet’s Service method.

Tomcat init

  • When Bootstrap is started using the./startup.sh script or the Java command, the Bootstrap entry point is the main method of the Bootstrap class.
  • The startup process is divided into two steps, namely init and start. This section focuses on init.
  • Initialize the class loader.
  1. Get a scan repository of class loaders by getting properties such as common.loader from the CatalinaProperties class. The CatalinaProperties class calls the loadProperties() method in its static block to load the properties from the conf/ Catalina.properties file (that is, the properties are loaded when the class is created).
  2. Create an instance of URLClassLoader with ClassLoaderFactory
  • Create an instance of Catalina using reflection and set parentClassLoader
  • SetAwait (true). Set the await property of Catalina to true. At the end of the Start phase, if this property is true, Tomcat listens for the SHUTDOWN command in the main thread. The default port is 8005. After receiving the command, run the stop() method of Catalina to shut down the Tomcat server.
  • CreateStartDigester (). Catalina’s method creates an instance of Digester and adds RuleSet to parse conf/server.xml. Digester was originally an open source project for Apache that parsed XML files, but I saw that tomcat-9.0.0. M22 incorporated these classes directly into Tomcat instead of introducing JAR files.
  • The parse() method is Digester’s handling of the conf/server.xml component creation process. The value is that these components are created using reflection. In particular, when creating Digester, we added some special rule sets to create some very core components that are not included in conf/server.xml, but are quite useful.
  1. EngineConfig. LifecycleListener implementation class, which is called when an Engine lifecycle event is triggered. This listener does nothing but print a log
  2. HostConfig. LifecycleListener implementation class that is called when the Host lifecycle event is triggered. The purpose of this listener is to deploy the application, which includes all Context XML files in the conf/// directory and applications in the webapps directory, whether war files or unzipped directories. The listener is also responsible for the hot deployment of applications by background processes.
  3. ContextConfig. LifecycleListener implementation class, called when the Context’s lifecycle event is triggered. This listener configures the application. It reads and merges conf/web. XML and the application’s web. XML, analyzes the annotations of Class files in/web-INF /classes/ and/web-INF /lib/*.jar. All servlets, ServletMapping, filters, FilterMapping, and listeners are configured into StandardContext for later use. Of course, there are other application parameters in web.xml that are eventually configured into StandardContext.
  • ReconfigureStartStopExecutor () is used to reconfigure the start and stop the Executor of children. The default is 1 thread. We can configure the startStopThreads of Engine in conf/server.xml to specify the number of threads used to start and stop child containers, If 0 is configured, runtime.getruntime ().availableProcessors() is used as the number of threads. Use runtime.geTruntime ().availableProcessors() + if the sum is less than or equal to 1, use 1 as the number of threads. When the number of threads is 1, use InlineExecutorService, which directly uses the current thread to start and stop operations, otherwise use ThreadPoolExecutor, whose maximum number of threads is our configured value.
  • Note that the init for Host is done in the Start phase, and the default state property for StardardHost is lifecycleState.new, So an initialization is done before it calls startInternal().

Tomcat Start[Deployment]

  • The StandardHost Start StandardContext step in the figure is actually skipped in the actual execution process, because conf/server. XML does not have any Context configured, So findChildren() returns an empty array when it looks for the child, so the for loop that starts the child after iterating through the child is skipped.
  • Trigger Host’s BEFORE_START_EVENT life cycle event and HostConfig calls its beforeStart() method to create the CATALINA_BASE/webapps& $CATALINA_BASE/conf/// directory.
  • Trigger Host’s START_EVENT life cycle event, and HostConfig calls its start() method to deploy the application already in CATALINA_BASE/webapps & $CATALINA_BASE/conf//.
  1. Parse all XML files defining Context in $CATALINA_BASE/conf// and add to StandardHost. These XML files are called application descriptors. Because of this, you can configure a virtual path to save images used in your application. For details, see the Development Environment Configuration Guide – 6.3. Configure the image directory
  2. Deploy all WAR files under $CATALINA_BASE/webapps and add to StandardHost.
  3. Deploy all unzipped directories under $CATALINA_BASE/webapps and add to StandardHost.

In particular, when added to StandardHost, the start() method of StandardContext is called directly to start the application. See the Context Start section for the steps to Start the application.

  • When StandardEngine and StandardContext are started, their respective threadStart() methods are called, which creates a new background thread to handle background events for that container and its children and components within the container. StandardEngine creates a background thread directly, StandardContext is not created by default and is shared with StandardEngine. The background threading mechanism calls the component’s backgroundProcess() method periodically. See the Background Process section for details.
  • MapperListener
  1. The addListeners(engine) method adds this listener to StandardEngine and all of its child containers
  2. RegisterHost () registers all hosts and their child containers in the Mapper for later request processing.
  3. When a new application (StandardContext) is added, the Host’s container event is raised, and the mapping of the new application is registered with Mapper via MapperListener.
  • After all the work is done, Catalina will create a Catalina Hutdownhook and register it with the JVM. CatalinaShutdownHook inherits Thread and is Catalina’s inner class. Catalina’s stop() method is called directly in its run method to shut down the entire server. The reason for registering this Thread with the JVM is to prevent users from terminating Tomcat improperly, such as directly closing a command window. When the command window is closed directly, the operating system sends a termination signal to the JVM, which then starts the registered Shutdownhooks one by one to shut down the resources before exiting.

Context Start

  • The StandRoot class implements the WebResourceRoot interface, which holds all the resources of an application, in layman’s terms, all the resources deployed to the corresponding Context directory in the Webapps directory. Because I am not very interested in the resource management part of Tomcat for the time being, I only did a brief understanding of the resource management related classes and did not delve into the source code.
  • The resourceStart() method does the initial configuration of the StandardRoot
  • PostWorkDirectory () is used to create the corresponding working directory, $CATALINA_BASE/work///, which is used to store temporary files.
  • StardardContext is just a container, while ApplicationContext is the actual running environment of the application, and the related classes and operations will be supplemented after the request processing process is done.
  • StardardContext fires the CONFIGURE_START_EVENT life cycle event, and ContextConfig starts calling configureStart() to configure the application.
  1. This procedure parses and merges the configuration in conf/web.xml & conf// web.xml.default & webapps//WEB-INF/web.xml.
  2. Configure the parameters in the configuration file to StandardContext, mainly Servlet, Filter, Listener.
  3. Because annotations are directly supported since Servlet3.0, the server must be able to handle annotated classes. Tomcat registers scanned servlets, filters, and Listerner with StandardContext by analyzing Class files in WEB-INF/classes/ and jar packages in web-INF /lib/.
  4. SetConfigured (true) is a key action that identifies the successful configuration of the Context. If this is not set to true, the Context will fail to start.

Background process

  • The purpose of the background process is to handle periodic events in the Servlet engine, with a default processing interval of 10 seconds.
  • The special StandardHost backgroundProcess() method triggers the Host’s PERIODIC_EVENT lifecycle event. HostConfig then calls its check() method to reload the loaded and redeployed application or to hot deploy the newly deployed application. The reload() procedure simply calls setPause(true), stop(), start(), and setPause(False) in sequence, where setPause(true) temporarily stops accepting requests.

How to read excellent open source projects

Really the first time to read the source code of open source project, the harvest is great. It made me better at architectural design, object-oriented thinking, design patterns, Clean Code, and so on. Reading about great open source projects is fun, because every once in a while you’ll find a new design idea and wonder how it’s still possible!

Of course, there are still some pain points when reading, such as hitting a variable and failing to Find the initialization location, sometimes using the Find Usage tool, but other times you have to go through the source code from scratch. Sometimes a design idea can not understand why it is designed and so on, this situation can only be solved by analyzing a higher level of architecture and so on.

Let me briefly share how I read the source code for open source projects.

  • First look for some books to introduce the project architecture, the project architecture is the core of the core of the project, read the architecture to read high-level design ideas, read the source code to read low-level implementation details. With high-level design ideas to do guidance, source code reading will be handy, because when reading the heart is very clear now reading the source code in the whole project structure in what position. I passed the book How Tomcat Works before reading the Tomcat source code, and then looked at the second chapter of The Tomcat Architecture Analysis to get a preliminary understanding of the Tomcat architecture. (PS: “How Tomcat Works” is written in English but reads very smoothly. Although it is based on Tomcat 4 and 5, the Tomcat architecture does not change very much. The new version of Tomcat only adds some components. This is the first book!
  • If you can’t find a book on architecture, make your own class diagrams! Generally speaking, open source projects are for the purpose of providing services, we take the process of providing services as the main line to analyze the source code, so that the purpose will be stronger, the process involved in the class drawn into the class diagram, the final class diagram is the architecture! But before you analyze, you need to find the entry point of the process, otherwise the analysis can’t begin. Taking Tomcat as an example, its main flow can be roughly divided into three parts: startup, deployment, and request processing. Their entry points are the Bootstrap class and the Acceptor class that accepts requests!
  • With that in mind, let’s talk about tools. I use the reading tool is IntelliJ IDEA, a very powerful IDE, may be more heavyweight, if you have other more lightweight Linux platform source code reading tool, can recommend to me ~
  1. The Structure column allows you to customize the list of fields and methods in a class, and then group the fields and methods by inheritance Structure, so that you can directly see which class in the inheritance Structure the fields and methods are defined in. When you click on methods and fields, you can automatically scroll to source code and so on.
  2. Right-click in the source code -> Diagrams -> Show Diagram to show the inheritance structure of the class, including all the ancestors of the class and all the interfaces. In this figure, select the specified parent class and interface, right-click -> Show Implementations, IDEA will list the implementation class of the interface or subclasses of that class.
  3. FindUsage, Go To Declaration, etc.

The Reference www.amazon.com/How-Tomcat-… Product.dangdang.com/25084132.ht… Tomcat.apache.org/tomcat-9.0-… Www-eu.apache.org/dist/tomcat…

Recent hot articles recommended:

1.1,000+ Java Interview Questions and Answers (2021)

2. I finally got the IntelliJ IDEA activation code thanks to the open source project. How sweet!

3. Ali Mock is officially open source, killing all Mock tools on the market!

4.Spring Cloud 2020.0.0 is officially released, a new and disruptive version!

5. “Java Development Manual (Songshan version)” the latest release, quick download!

Feel good, don’t forget to click on + forward oh!