Source code analysis from Tomcat gets more - Tomcat

xiaoxiao2021-03-06  55

1. About Tomcat's basic situation

It is well known to Tomcat is a free open source Servervet container, which is a core project in the Jakarta project of the Apache Foundation, and is also the official SERVLET and JSP container recommended by Sun, and it has also gained a variety of honors. The latest specifications for servlet and JSP can be implemented in the new version of Tomcat. Tomcat has the advantages of lightweight and flexibly embedded in the application system, so it has been widely used. In Tomcat's development, Sun announced in June 1999 to participate in the Tomcat Servlet container and JSP engine developed in Jakarta project in June 1999, making Tomcat's large variation in system design between 3.x and 4.x. I don't have much to say other information about Tomcat. Interested friends can access http://jakarta.apache.org/ 's official website to get more information.

Because of the reason, I have rewritten some of Tomcat's code, so I roughly study the source code of Tomcat3.3 and Tomcat4.0, which is deeply attracted by this open software, and it feels a lot. It is worth learning and learning from the place. I introduce my understanding to everyone, I have a throwing brick, lack and deviation, I hope everyone criticizes. Let's take a look at what we can get from Tomcat.

2. Learn design mode from Tomcat

The design and implementation of Tomcat reflects the idea of ​​design model. Its basic process is first to analyze the configuration files in the XML format, obtain the system configuration and application information, and then load custom component modules to provide a variety of system services. The various features of the system are implemented by the components modules that can be configured. Tomcat implements a variety of design models such as OBServer, Facade, Adapter, Singleton, which can be seen everywhere in Tomcat's source code, which provides us with a good platform for learning design patterns. I mainly introduce the design model used in the program process control in Tomcat, which is a framework running. As mentioned earlier, Due to Sun Company's participation, Tomcat has no change in the basic process, but Tomcat3.3 and Tomcat4.0 versions are conceptually different. Tomcat3.3 is a modular design, while Tomcat 4.0 can be seen as a component technology for design. Components are a higher level than module. We can understand the design patterns and implementations that you can adopt by comparing them.

2.1Tomcat3.3 basic structural design

Tomcat3.3 uses a modular chain control structure, which is:

Chain of Responsibility (Responsible Chain)

As a server based on request response mode, a chain processing is used in Tomcat3.3. The request is delivered on each link on the chain, and there may be several "listeners" processing it on either link. The purpose of this is to avoid direct coupling between the requested sender and the recipient, so that other objects can provide a chance to participate in processing requests. This means can not only achieve system functions through the "listener", but also enable system functions to achieve expansion of system functions by adding new "listener" objects.

Interceptor (listener)

"Listener" is a name that has been used in the past, which can be seen as synonyms of "Module". It is a way of Tomcat function modules build and extension. Most of Tomcat3.3 is implemented through the "listener". A simple hook mechanism is provided in Tomcat, and the listener registers the events of interest in the hook. After the event, the registered "listener" object is woken up, "listener" object is the Tomcat core. The event is processed. These modules are designed around the "Responsible Chain" and "Policy". With "listener" you can monitor a variety of special events, and control each step of processing the request --- parsing, authentication, authorization, session, response, buffer submission, etc. STRATEGY (strategy)

The so-called strategy refers to "Defines a set of rules, and the object is encapsulated in accordance with the rules so that they only interact inside the rules." The development and evolution of Tomcat as an open source project is easier to develop and evolve as an open source project. The complex algorithm is divided into a module through this mode and then different development groups provides their respective implementations. Thus, the specific implementation of the code of the calling module and the specific implementation code of the module is implemented. This allows us to focus on the key points of the problem and reduce the dependence between issues. A large number of strategic design patterns in Tomcat, each of which provides a variety of implementations (such as 2-3 certification modules in Tomcat), which can be expressed from stability and performance after the code is completed. Consider choosing a better implementation. Strategy Mode is very useful for software development in an open environment.

We describe how Tomcat3.3 is implemented by listening and responsibility chain by simplified class diagrams (see Figure 1).

Figure a simplified class diagram

Brief description of the class diagram:

BaseInterceptor: It is the base class of all listeners describes basic module functions and default processing for various events.

CONTEXTMANAGE: The core control object of the system, performs request processing and system configuration. It maintains a global attribute, the content of the web application, and a global module and other information, the hook implementation of the chain is also there.

PoolTcpConnector: A connector object that handles TCP connects from BaseIntercePor. It includes a pooltcpendpoint object that specifics the Socket connection.

PoolTcpendPoint: Processes the actual TCP connection. It has a connection pool object threadpool and an application logic class TCPWORKTHREAD running in standalone thread.

TCPWORKTHEAD: Hands the Socket connection transaction, call the request processing method in the interface TCPConnectionHandler.

Http10Interceptor: Derived from PoolTcpConnector to implement the TCPConnectionHandler interface, is a real listener object. It processes the TCP connection in accordance with the protocol standard of HTTP 1.0, calls the service method of the core object ContextManage.

Figure 2 Simplified timing chart

About the places in the timing diagram:

After ContextManager initialization, the hook mechanism is created according to the configuration information, the basic application module and various listener objects, and the registration listener object is created, and a responsible chain is formed. Then eNGineinit, EnginesTart message is then issued to each listener object. A request is submitted to the ContextManager process after passing the HTTP10Interceptor basically processed. The processRequest method for ContextManager performs the process of request. H_PostReadRexTmap, H_RequestMap, etc. will be sequentially issued in the process of processing. Then acquire the listener object registered with the message from the hook, and call their processing method to implement the liability chain. The following code snippet illustrates this approach: BaseInterceptor ri []; // get registration object

Ri = defaultcontainer.getInterceptors (container.h_postreadRequest);

// Perform a method of processing a message for a registration?

For (int i = 0; i

The ContextManager issues the EnginesTop message when the system exits.

The basic program structure of Tomcat3.3 is designed by the way. It provides us with a good idea to easily implement an event-driven modular design, through this model. Each function is implemented through module, and the TOMCAT function can be extended by modifying the message and processing steps on the responsible chain or adding a new listener object. So this is a very good design.

2.2TOMCAT4.0 basic structure design

Although Tomcat3.x has already achieved a very good design system, Different implementations are also introduced in Tomcat 4.0 after Sun Company is added. The main difference is that Tomcat 4.0 uses components-oriented design, and the function in Tomcat4.0 is provided by the component, and the control process is completed by the communication between components. This is different from the module-based chain control structure in Tomcat3.3.

As a component-oriented technology (CO) is an abstraction than the object-oriented technology (OOP), which combines the object-oriented advantages, joining security and scalable module design, which can make better mapping problem space . The use of components will bring a lot of benefits, improve reuse, reduce coupling, and through assembly systems, and the like. Many concepts facing components are different from object-oriented programming, for example:

Message: Define abstract operation; method: Define specific operations; interface: a set of messages; importation: a set of methods; Module: Static data structure , TYPE: Dynamic data structure.

Software components differ from the functional module, which has the following features:

Components are a self-contained module with a defined boundary, providing its capabilities, attributes, and events. The component itself does not retain the state. Components can be a class, most of which is a set of classes.

Support for component-oriented programming is obtained through the JavaBeans model in the Java language. The JavaBean Component Framework provides support for events and properties. The components of Tomcat 4.0 are implemented through JavaBean technology. This is the biggest difference in it and Tomcat3.3. Let's take a look at how Tomcat 4.0 is controlled by component programming.

The design component is critical as component programming, which can be seen from Tomcat 4.0: Separation of Concerns (SOC)

When designing components, from different problems, stand at different views, respectively consider each attribute. For example, the Filelogger component is used to save the system log information into the file system. According to this mode analysis, we look at it from different angles: How do it start the service, stop the service and communicate? What are its specific features? What messages can other components can be sent? Based on these considerations, FILELOGER components should implement two interfaces: Lifecycle (survival interface) and LoggerBase (function interface).

Inversion Of Control (IOC) This mode is defined that the components are always managed outside. The information required by the component is always from the outside, and the actual components are managed by each phase of the survival period. In Tomcat 4.0, each function is implemented through mutual control and calls between such components.

The components in Tomcat 4.0 obtained in these modes are both common and characteristic. The commonality is a LifecyCle interface, and features are different functional interfaces. The events of each phase of the entire survival of the survival interface processing components are provided to other components.

The specific function is how I am not introduced here, I mainly introduce the design of the LifecyCle interface of the component in Tomcat 4.0. The LifecyCle interface implements the surplus management, control management, and communication of components. To create a software component and create a JavaBean object, you can refer to JavaBean to understand. I will describe it through a class diagram of analog LifecyCle interface components. (See Figure 3)

Figure 3 Lifecycle interface component class diagram

Description of the class diagram of the simulated LifecyCle interface component

Lifecycle Interface Defines Message for a set of component communication. The component implements a Lifecycle interface, defines a LifecyClesupport object inside the component. Other components that need to communicate with the component must implement the LifecyCleListener interface, which manages other components that require communication via the Add / RemovelifeCyleCleListener method. When the components are required to communicate with other components, they are notified of the other components when needed with other components. Other components get notifications through the LifeCyclevent method. The LifecycleEvent object is derived from java.util.eventobject. Of course, in component design and implementation, we can also use the classes already provided in JavaBeans such as Java.Beans.PropertyChangeListener; java.beans.propertyChangeSupport can get more functional feature support.

Through the above analysis we can see the core concept of the component becomes Tomcat 4.0, the functionality of the system is implemented by components, and communication between components constitutes a system running control mechanism. We compare the modular chain control mechanism in Tomcat3.3 and the design of Tomcat 4.0, which will find Tomcat4.0's concept of software components in design ideas. The main difference between Tomcat 4.0 and Tomcat3.3 is here. As for object-oriented and assemblies, I don't introduce here, interested friends can find a lot of resources in this area.

3. Efficient software components from Tomcat Source Codes

Tomcat not only provides us with new ideas when designing and implementing system, because it is composed of components or modules, so it also provides us with a large number of high-efficiency software components. These components can be used in our program development. I simply listed some, you can take it directly from the source code directly. Some special set data structures such as pools, queues, caching, etc. can be used for server development. / src / share / org / apache / Tomcat / Util / Collections A simple hook (hooks) mechanism implementation. SRC / Share / ORG / Apache / Tomcat / Util / Hooks Realization of ThreadPool. SRC / Share / ORG / Apache / Tomcat / Util / Threads Component Lifecycle interface design and implementation. / src / catalina / src / share / org / apache / catalina is implemented for the implementation of loggers. SRC / Catalina / Src / Share / Org / Apache / Catalina / Logger is implemented for XML format configuration information for the XMLMAPper. SRC / Catalina / Src / Share / ORG / Apache / Catalina / Util / XML Advanced Management and Implementation for Socket Communication (NET). / src / catalina / src / share / org / apache / catalina / net

Through the simple introduction of Tomcat, we can see that as an open source project, Tomcat not only provides us with a platform for the application, but it also provides us with a learning and research design mode, component technology, etc. The theoretical practice platform.

Reference

Tomcat3.3 source code and tomcat4.0 source code http://jakarta.apache.org/tomcat/index.html "Design Mode"

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

New Post(0)