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 "
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,:
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
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