Tomcat Source Code Analysis (Startup Framework)

zhaozj2021-02-16  61

Tomcat Source Code Analysis (Startup Framework) Preface: This article is some of my experience after I read Tomcat source. Mainly to explain the Tomcat system framework, as well as the startup process. If there is a mistake, please criticize the advice! Recommendation: After all, Tomcat's frame is still more complicated. Single is understood from text, it is not so easy to master Tomcat framework. Therefore, we have practical, practice, and practice. It is recommended to download a Tomcat source code, debug pass, and then track its startup process. If you don't understand, come and see this article and see if you can get help. I believe that this effect and the speed of learning will be a lot! 1. Tomcat's overall frame structure Tomcat's basic framework is divided into 4 levels. TOP Level Elements: Server Service Connector HTTP AJP Container Engine Host Context Component Manager Logger Loader Pipeline Valve ... Standing on the top of the frame is Server and Service Server: actually the BackGroud program, the use of Server in Tomcat is startup and listening Server events (such as restart, close, etc. in Tomcat standard configuration file: server.xml, we can see "" here " "Shutdown" is the command word used when Server is listening to the server event) Service: In Tomcat, Service refers to a solution for a class. Usually we will use Tomcat-Standalone mode for the Tomcat-Standalone mode by default. The service in this manner is given to us to provide the services of the JSP and Servlet, and it is also available to resolve the service of static text. CONNECTOR: Tomcat is handled in the container, and where to get the input information again? The Connector is the exclusive. He will pass the data passed from the socket, package into a request, and pass to the container. Usually we will use two CONNECTORs, a kind of HTTP Connectoer to deliver HTTP needs. Another name called AJP, when we integrate Apache and Tomcat, Apache and Tomcat are interacting through this protocol. (Saying the integration of Apache and Tomcat, usually our purpose is to let Tomcart to resolve dynamic JSP or servlet.) Container: When HTTP Connector passes demand to top CONTAINER: Engin Our sight should be moved to the Container level. In the Container layer, we contain 3 containers: Engin, Host, Context. Engin: Received the requirements of Service passing, after processing, return the result to the service (Service is interacting with ENGIN through the Connector). Host: ENGIN does not handle itself after receiving the requirements of Service, but is handed over to the appropriate Host to handle it.

Host is herein the meaning of the virtual host, usually we will only use one host, or both "localhost". Context: Host received the demand passing from Host, it won't handle itself, but is handed over to the appropriate context. For example,: The former handed over the context to handle it, the latter handed over Bar This context is processed. Obvious! Context meaning is actually a web app meaning. We usually do such a configuration in Server.xml This Context container is the place where we do what we should do of. Compenent: Next, we continue to talk about what Component is dry. We have to understand the relationship between the container and components. The demand is passed to the container, and it is passed to the next container processing when appropriate. The container is still in full contained components, and we can understand to provide a wide range of value-added services. Manager: When the Manager component is installed in a container, this container supports session management, in fact, SESSION management inside Tomcat is depends on the Manager Component in Context. Logger: When a container is installed Logger components After that, what happened in this container is recorded by this component! We usually see Catalina_log.time.txt and localhost.time.txt and localhost_examples_log.time.txt. This is because we have the three containers of Engin, Host, and Context (Examples), which is also the default installation, called standard :) loader: loader This component usually only uses our context container, Loader is used to launch CONTEXT and manage ClassLoader for this Context. PIPLINE: Pipeline is such a thing, when a container determines how to give the need to the child container from the superior, he puts this demand into the pipeline of the container. The demand is a distant call to intercept the valves inside the pipe in the pipeline. For example, two valves have been put in the pipe. The first valve is called "access_allow_vavle", that is, when the demand flows, it will see which IP is coming over, if this IP is already in the blacklist, Sure, kill! The second valve is called "defaul_access_valve" it will do a routine check, if passing, OK, passes the demand to the status of the current container. In this way, the demand is transmitted, flowing, and finally arrived at the destination in each container. Valve: It is the valve mentioned above. Tomcat is probably so something, we can simply understand the Tomcat framework, which is a structure in which the container is also included in the container.

2. Tomcat's startup process This article is how to start Tomcat. Since we substantially understand Tomcat's frame structure, then we can guess Tomcat's startup, will start the parent container first, then one by one Start the child container inside. When you start each container, you will start the components on him. When all the components are started, all containers are started, Tomcat itself is started. Transfer to the stage, we can also guess that Tomcat's startup will be divided into two parts, the first step is to assemble. The second step is to start working. Assembly work is to install a child container for the parent container, and install the work of the components for each container. This place we will use Digester mode, as for Digester mode, what is used, how to work. Please refer to The startup work is after assembly work Once the assembly is successful, we only need to ignite a wire, the entire Tomcat will be activated. This is like we have to open a car that has already been equipped. We just need to put the key into the keyhole, twist, the car's engine will start, the air conditioner will start, the safety device will take effect. In this way, the car will start. (This process is indeed unconscious with Tomcat's startup process, let us doubt Tomcat's designer is developed in GE as Java). 2.1 Some interesting names: Catalina Tomcat Bootstrap Engin Host Context They Means: Catalina: Remote Bomber Tomcat: Panda Bomber - A bomber (this reminds me of the panda mobile phone that makes the Chinese people leaders, is it English can be called Tomcat ???, let me think of another advertisement: Wave guide - fighters in mobile phones, Boeing - fighters in the airliner) Bootstap: Guide Engin: Engine Host: Host, Territory Context: Content, Goal, Context ... After many years, modern humans have been extracted. The post-modern creatures found these words to zero zero falling in one.

A self-thought-intelligent guy translated these things: under the bootstrap, a bombalina is shining, far from the panda bomber (Tomcat), close to the Panda bomber! With excellent engine technology (Engin), this panda bomber flew over the territory of the enemy (Host), and the CONTEXT cast a nuclear warhead destroyed the earth, the wave ~ modern creature is so fart ~ In summary, this has never been associated with GE is also involved in the production of military equipment? Against the US imperialism! Against the hegemony! Heaven is long! for freedom! 2.2 History is so amazing! Tomcat starts from org.apache.catalina.startup.bootstrap this class! Two things have been made in Bootstrap: 1. Specify 3 types of ClassLoader: CommonLoader: Common / Classes, Common / Lib, Common / Endorsed CatalinaAloader: Server / Classes, Server / Lib, CommonLoader SharedLoader: Shared / Classes, Shared / Lib, CommonLoader 2. Boot Catalina's startup. Use the reflection technology to call org.apache.catalina.startup.catalina's Process method and pass the parameters. 2.3 Catalina.java Catalina has completed several important tasks: 1. Assess the Tomcat of each container and component using Digester technology. 1.1 The main content of assembly work is to install each major piece. What kind of servcie is there in Server. How many context will Host will accommodate. Which components will be used in Contexts and more. 1.2 At the same time, in the step of assembly work, the configuration of MBeans is completed. Here, I simply describe what MBean is, what is used. The objects we have generated, managed by themselves, and the sky! But if we have created an object, I want someone to take the management, what should I do? I want to tell others what we have, and what methods can be found! JMX technology provides us with a means. There are three main things in JMX. MBean, Agent, Connector. MBean: It is used to map our object. Perhaps MBean is the object we created, maybe not, but with it, you can quote our object. Agent: You can find MBean by it. Connector: Connect the way of the Agent. Can be http, or the RMI, you can also pass the socket directly.

Tomcat things happen in the assembly process: the initialization is triggered GlobalResourcesLifecycleListener class: protected static Registry registry = MBeanUtils.createRegistry (); run MBeanUtils.createRegistry () will be based on / org / apache / catalina / mbeans / mbeans-descriptors. XML This configuration file creates MBeans. OK, the outside world has a path access to each component in Tomcat. (A bit like latter) 2. Initialization work for the Server of Top Level. In fact, it is usually configured to both Connector. (Http, ajp) 3. Start from Server, ignite the entire Tomcat. 4. Do a hook program for Server, check when Server Shutdown, close Tomcat Each container is used. 5. Monitor 8005 port, if you send "Shutdown" (default, strings) come over, close 8005Serversocket. 2.4 Launch Each Container 1. Server triggers the Server container starts before starting (Before_Start), starts (start), starts (after_start) 3 events, and run the corresponding event processor. Start Server Subject: Servcie. 2. Service Sub-container: Engin launches Connector 3. EngN into Engin, and the following level of container, Tomcat uses a comparison startup method. First, run each container own task Subsequently, the trigger startup event immediately, set the label, indicating that the container has been launched, launching the various components in the container: LOADER, Logger, Manager, etc., then launch the mapping component. (Note 1) Tightly followed, promoter the sub-container. Next, the pipeline of the container is started and then the event is triggered, and the event is triggered after the startup event. ENGIN is roughly doing this, Host will do this, and Context is roughly doing this. Obviously, we need to use the code multiplexed here. Tomcat uses an abstract class to handle it when processing this problem. ContainerBase. Finally, this part of the complex function code is clean and profitable. It is really sighful. It is really sighing. In order to enjoy Jiazhen, the other is cheeks, nostalgic! ENGIN's trigger startup event, activate the only listner: enginconfig that is bound to engin. This ENGINCONFIG class basically doesn't do anything, that is, set the debug level of EnginConfig to ENGIN. In addition, it is to output a few lines of text, indicating that Engin has been configured and does not do substantive work.

Note 1: The use of Mapping components is when a requirement will be transferred from the parent container to the child container, while the parent container has multiple sub-containers, which sub-container should be selected to handle the demand? This will be determined by the mapping component. 4. Host is the same as ENGIN, but also the start () method in which ContainerBase is called, but the task of doing some self, it is the channel of the HOST container (Pipline), which has been installed "org.apache.catalina. Valves.ErrorReportvalve. Valve. The use of this valve is such that the demand will continue to pass to Context to do specific processing after being passed to HOST by Engin. The demand here is actually the Request, Response, which is passed as a parameter. Therefore, after Context handles the demand, the response will usually change. This org.apache.catalina.valves.ErrorReportvalve is to verify whether RESPONSE contains errors, if there is a corresponding process. 5. Context arrived here, finally turned into Tomcat to start the real weight show, starting context. StandardContext.Start () This method of launching the Context container is called by StandardHost. 5.1 WebAppResources This Context pointed at the specific directory 5.2 Installing DefaultContex, DefaultContext is the default context. If we install DefaultContext below, and DEFAULTCONTEXT has installed a database connection pool resource. Then all other Contexts under this Host can use this database connection pool directly without having to be configured. 5.3 Specify Loader. Usually use the default org.apache.catalina.Loader.Webapploader class. Loader is used to specify which classes will be used, what is the JAR package? 5.4 Specify manager. Usually use the default org.apache.catalina.session. StandardManager. Manager is used to manage session. In fact, the management of sessions is also very good. Take a simple session management as an example. When the demand passes, there is a sessionID attribute in the Request object. OK, after getting this sessionID, we can use it as a key, while we can place a hashmap. Hashmap, let us put things. 5.5 PostWorkDirectory (). Tomcat There is a work directory. We throw the temporary documents there. This step is to create a directory there. Generally speaking, you will generate a directory in% CATALINA_HOME% / Work / Standalone / localhost /. 5.6 Binding Thread. In the case of it, Class Loader should be interchanged. It is before you see all Class and Lib below Tomcat. Next, you need to see the Class of Class under the current context. So set up CONTEXTCLASSLOADER, but also record the old ClassLoader because it will be used later. 5.7 Start loader. Specify which classes to use this Context specifically to use which JAR files.

If RELOABLE is set to True, start a thread to monitor Classes changes, restart the context if there is change. 5.8 Starting Logger5.9 Trigger a listener installed on it. Lifecycle.firelifecycleEvent (start_event, null); as one of the listeners, ContextConfig will be activated. ContextConfig is used to configure web.xml. For example, how many servlets have this context, and how many filters are installed here for Context. 5.9.1 DefaultConfig. Each context has to configure this file with Tomcat / Conf / Web.xml. 5.9.2 ApplicationConfig Configuring your own web-inf / web.xml file 5.9.3 ValidateSecurityRoles permission verification. Usually we are accessible when we are accessing / admin or / manager, or you can access. And we can also limit those resources that can be accessed, and which can't. It is achieved here. 5.9.4 TLDSCAN: Scan, what labels need to be used (Tag Lab) 5.10 Start Manager5.11 PostwelComeFiles () We usually use the name of the 3 boot files: index.html, index.htm, index.jsp Ball it by default to this Context 5.12 listenerStart Configuration Listener 5.13 FilterStart Configuration Filter 5.14 Start with 1 SERVLET. The order is from small to large: 1, 2, 3 ... Finally 0 By default, at least the following three servlet: org.apache.catalina.servlets.defaultServlet handles the servlets of static resources. What pictorial, html, CSS, JS are looking for him org. Apache.catalina.servlets.invokerservlet handles those servlet mapping those servlet. Org.apache.jasper.servlet.jspservlet handles JSP files. 5.15 Identifier Context has been activated. How many steps have you gone? Context is finally started. OK! Go here, each container and components are started. Tomcat is finally served for the people! 3. Reference: 4. Postscript The article is to explain the Tomcat launch frame, and there is a detail of the message processing process in Tomcat. The content of the article is already written, and it is now finishing the stage. I believe it will soon make it, and everyone will study together. This article is written by the Tomcat source code alone, so there must be some subjective color, which will inevitably have a side. If you have inappropriate, please criticize the advice, which can not only make the brothers who have just started to study Tomcat less, I can learn something. Email: sojan_java@yahoo.com.cn5. Tomcat Source Code Analysis (Message Processing)

转载请注明原文地址:https://www.9cbs.com/read-21849.html

New Post(0)