The component structure of Tomcat research

Source: Internet
Author: User
Tags definition event listener functions implement interface wrapper domain name tomcat

The component structure of Tomcat research















Hello, this article introduces the ClassLoader of Tomcat research, this article introduces the component structure



It would be very difficult to study the Tomcat kernel without any material information, but in any case there are opensource, at least we can track Tomcat's hundreds of classes and we have to keep working on it so we can completely unlock Tomcat's veil.



It's not hard to see through UML class diagrams that the most important class in the core package is containerbase, and this abstract class implements container, Lifecycle, Pipeline, Mbeanregistration, Serializable interface, serializable interface we are very familiar with the first few interfaces, because we suspect that the entire tomcat part of the architecture may be based on these several basic interfaces.







Before entering the analysis, let's recall the Config.xml (server.xml) file, from this file and the Apache documentation we can generally see that Tomcat has the following components on the macro:



-The server server element is the entry point for the JVM and there is only one entire configuration file, because the server is not a container (container) and therefore cannot be nested. Server listens for shutdown commands on a specified port. Server can contain one or more A service real.







-Service service is composed of one or more connectors that share the same container, the General Service is a engine, but there is no clear specification requirement. Because Service is not a container, so it cannot be in Face-nested subassemblies (such as loggers/valves).







-Connector Connector is a tomcat connection to the client, and Tomcat has two typical connector:http,jk2.http Connector listening for connections from browser (usually on our familiar 8080 port ), JK2. Requests from other webserver (the default is listening on port 8009). Connector will hand over the request to engine for processing.







-Engine Engine can be configured with more than one virtual host, each virtual host has a domain name when Engine obtains a request, it matches the request to a host and then hands the request to the host for processing Engine has a default virtual host , when the request cannot be matched to any host, the default host is given to handle the







-Host host represents a virtual host, the default is Localhost,host can deploy multiple Web application, in our practical application of the general to consider the problem is host















1. org.apache.catalina.Lifecycle







The generic component declares the cycle interface, which is typically implemented by Tomcat components (but not necessarily), which provides the same start and stop for all components. The main methods are:







Add a listener







public void Addlifecyclelistener (Lifecyclelistener listener);















/**







* This method should be invoked before any public method is invoked







* This method sends a Start_event event to all registered to







* Listener for this component







*/







public void Start () throws lifecycleexception;















/**







* This method should be invoked after all public methods have been invoked







* This method sends a Stop_event event to all registered to







* Listener for this component







*/







public void Stop () throws lifecycleexception;















2. Org.apache.catalina. Lifecyclelistener







This interface is used to listen for important events (including implementation of the Start,stop event generated by the Lifecycle Interface component)







The main methods are:







Handling of events heard by supervisors







public void Lifecycleevent (Lifecycleevent event);























3. Org.apache.catalina.Container







A container is an object that is used to obtain a request from the client and process the request and reply to the client (response). Containers can support (optionally) pipeline so that requests can be processed in a configured order at run time.







Inside Tomcat, the container is conceptually the following and the layers:







Engine Request processing entry point, can contain multiple host and context







Host represents a virtual host







The context represents a single servletcontext and can contain multiple wrappers







Wrapper represents a single servlet, and if the servlet implements Singlethreadmodel, it can represent multiple instances of a single servlet.







Containers often bind to other components in order to implement their functions, and the functions of these components may be shared or individually tailored, and the following components are used:







Loader ClassLoader, loading Java Classes







Logger implements the ServletContext log method for logging







Manager manages the session pool bound to the container







Realm User security Management







Resources Jndi Access







The main approach:















Add Container Listener







public void Addcontainerlistener (ContainerListener listener);















Add Property Listener







public void Addpropertychangelistener (PropertyChangeListener listener);















/**







* Process request and produce corresponding response







Requests processed by *param request







The response produced by *param response







*/







public void invoke (Request request, Response Response) throws IOException, servletexception;















4. Org.apache.catalina. ContainerListener







Container event Listener, note that start,stop is a normal life cycle event (liftcycleevent) is not a container event. Main methods:















Handling Container Events







public void ContainerEvent (ContainerEvent event);







5. Org.apache.catalina. Pipeline







Pipleline is a collection of valve that, when invoked, invokes the valve in the order specified, and always requires a request (usually the last) that the valve must handle delivery and produces response. Otherwise, pass the request to the next valve.







Generally a container binds only one pipleline instance, and generally the container encapsulates the function of processing the request into a container-bound valve (this valve should be executed at the end of Pipleline). To accomplish this, Pipleline provides a setbasic () method to ensure that the valve is finally executed, while the other valve are invoked sequentially.







6. Org.apache.catalina.Valve







Valve is a request processing component that is bound to a container, and a set of valve is bound to a pipleline in order. One of the most important methods of valve is:















/**







* A valve may perform the following actions in a certain order







*1. Check and/or modify the specified request and response properties







*2. Check the Request property, generate the corresponding response and return control to the caller







*3. Check the request and Reponse properties, wrap these objects and enhance their functionality, and then pass them







To the next component







*4. Call Pipleline if the corresponding response is not generated (and control is not returned)







The next valve (if any) on is Context.invokenext by method ()







*5. Check (but not modify) the response property (generated by valve or container after the call)







*valve must not be doing the following.







*1. Change some of the attributes of the request (changing request properties that have







Already been used to direct the flow of processing control







For this request)







*2. Create a response that has been created and has been passed







*3. Modify the HTTP header information that contains response after calling the Invokenext () method and returning







*4. Make any calls on the output stream on the binding response after the Invokenext () call returns







* @param request will be processed







* @param response will be created response







* @param context is used to invoke the next valve valve context







public void invoke (Request request, Response Response,







Valvecontext context)







Throws IOException, Servletexception;







7. Org.apache.catalina.ValveContext







A valvecontext is a mechanism in which a valve can trigger the next valve call without having to know the intrinsic implementation of the mechanism.







8. Org.apache.catalina.Engine







Engine is a container that is the entry point of the Cataline servlet.







Engine may not be used when publishing a cataline that is connected to a Web server because Connectior will use the resources of the Web server to decide which context to use for request.







Sub-containers attached to engine may be host or context (a single servlet context) depending on the engine implementation.







If engine is used, it is the top-level container at the cataline level, so setparent () should throw illegalargumentexception exception. We can see the structure of the engine from the following methods:















/**







* Set the engine attached service







* @param service The service that owns this Engine







*/







public void Setservice (service service);















/**







* Set The default hostname for this Engine.







*







* @param defaulthost the new default host







*/







public void Setdefaulthost (String defaulthost);















/**







* Set the Defaultcontext







* For new Web applications.







*







* @param defaultcontext the new Defaultcontext







*/







public void Adddefaultcontext (Defaultcontext defaultcontext);















9. Org.apache.catalina. Host







Host is a container that represents a virtual host.







You may not use host when you publish a cataline that is connected to the Web server, because Connectior will use the resources of the Web server to decide which context to use for the request.







The parent container attached to the host is usually engine, and the child container attached to host is usually the context (a single servlet context).







Most of the methods in the host interface are about modifying the host property and setting the default context. Here we will not enumerate.







Org.apache.catalin. Context







The context is a container that represents a ServletContext, a single Web application in a Cataline engline. The parent container attached to the context is host, and the child container attached to the context is wrapper (representing a single servlet). The context interface is mostly about setting up Web application, and we can refer to the methods in the Web.xml file study, where most of the methods read the resources in the Web.xml file.







Org.apache.catalina.Wrapper.







Wrapper is a container that represents a single servlet. Wrapper manages the lifecycle of the servlet, including invoking the Init () and Destory () methods.







The parent container attached to the wrapper is the context, and there is no child container attached to the wrapper, and Method Addchild () should throw illegalargumentexception exceptions.







The method inside the wrapper interface is all about reading the attributes of the servlet, you can refer to the definition of <servlet> tags in the web.xml file.







Org.apache.catalina.Server







The server is the entry point for the entire Catalina container, and can contain multiple service and top-level resource elements. Classes that implement the server interface in general should also implement the lifecycle interface, calling the corresponding method of the service when the start () and Stop () methods are invoked.







The server is responsible for listening to the connection on the specified port, and when a connection is accepted, the server analyzes the first line of information in the request and, if it is shutdown, the stop service. You can refer to the Server.xml file's definition of server.







Org.apache.catalina. Service







A service is a collection of one or more connectiors shared with container.







The JVM can contain one or more service instances, but they are completely separate from each other, and they share only the resources of the JVM.







Org.apache.catalina. Connector







Connector is a component that accepts requests from the client (request) and generates a response (reponse). A connection usually performs the following logic:







1 accepting requests from client programs







2 Create the request and response and set the following properties to these objects







(1) All Request,connector,protocol,protocol,response,scheme,secure,servername,serverport,serverport properties must be set. Contentlength,contenttype is also usually set.







3) for all the Httprequests,method,querystring,requestedsessioncookie,requestedsessionid,requestedsessionurl,requesturi , the secure attribute must be set. In addition, all addxxx methods must also be tuned to record cookies,headers and locales information.







4 Connector,request,stream properties must be set for all responses properties.







For httpresponses, connector does not set additional headers information for it.

1. Tomcat overall architecture class diagram

























The next chapter will introduce Tomcat from the various components of Tomcat.


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.