DOCX

TOMCAT class interface design life cycle

By Stacy Greene,2015-04-19 01:32
17 views 0
TOMCAT class interface design life cycle

    preface

    From the server. The XML document parsing out every object is a container, such as server, Service, Connector, etc.These containers are new, initialization is complete,start, stop, failure, destruction, etc.Tomcat implementation provides life cycle management of the container, this article through to the Tomcat7.0 source reading, in-depth analysis of the process.

    Tomcat class interface design life cycle

    We read the figure 1, learn about Tomcat involves the lifecycle management of the main class.

Figure 1 Tomcat class interface design life cycle

    Here the main classes in figure 1 that there were a simple introduction: ; Lifecycle: life cycle defines the container, the container state

    transition and container state transition event listener

    registration and remove such main interface;

    ; LifecycleBase: as a Lifecycle interface implementation class,

    abstract using the abstract template pattern will link up all

    containers of life cycle and the state transition, and also provides

    the generated LifecycleEvent event interface;

    ; LifecycleSupport: provide relevant LifecycleEvent event listener

    registered, removed, and the use of classical listener mode,

    implement event generated after the contact for the realization of

    the listener;

    ; MBeanRegistration: Java JMX framework provides the registration of

    an interface, the introduction of this interface is provided in

    order to facilitate using JMX management function;

    ; LifecycleMBeanBase: Tomcat provides the abstraction of

    MBeanRegistration implementation class, using the abstract

    template pattern will all containers unified registration to the

    JMX;

    In addition, ContainerBase, StandardServer, StandardService, WebappLoader, Connector, StandardContext, StandardEngine, StandardHost, StandardWrapper containers are inherited LifecycleMBeanBase, so these containers have the same life cycle and can be conducted through JMX management.

    What is a JMX?

    Java management extensions (Java management extensions, JMX for short), is a Java application or system into the framework of remote management functions.For ease of interpretation, I from the Internet to find a JMX architecture, as shown in figure 2.

Figure 2 JMX architecture

    Here introduces three layered in figure 2:

    ; The Probe Level: be responsible for testing (information) resources,

    contain MBeans, usually also called Instrumentation Level.MX

    management component (an) is divided into four forms, respectively

    is a Standard management component, an), Dynamic management

    artifacts (Dynamic an), Open management artifacts (Open an) and the

    Model management component (Model an).

    ; Agent Level: the MBeanServer, is the core of JMX, connects the

    Mbeans and applications.

    ; Remote Management Level: by connectors and adaptors to Remote

    operation MBeanServer, commonly used console, such as JConsole,

    VisualVM.

    The container

    Tomcat container of

    StandardServer, StandardService, Connector, StandardContext these container, have a father and son relationship between each other, every container can contain zero or more child container, these children may exist different types or the same type of multiple, as shown in figure 3.

    Figure 3 Tomcat container

    State of the Tomcat container

    At present, the Tomcat container with the following condition:

    ; NEW: the container has just been created, be in namely LifecycleBase

    instance state of structure is complete.

    ; An INITIALIZED: container initialization is complete.

    ; STARTING_PREP: beforestarting the container status.

    ; In the process ofSTARTING: containerstartup state.

    ; STARTED: containerstartup state.

    ; STOPPING_PREP: container stopped in front of the state.

    ; In the process of STOPPING: container stop state.

    ; STOPPED: container stop state.

    ; DESTROYED: after the destruction of the container.

    ; FAILED: the container in the process ofstart, stop, abnormal

    condition.

    ; MUST_STOP: this state is not used.

    ; MUST_DESTROY: this state is not used.

    These states are defined in the enumeration type of LifecycleState.

    Events and listening

    Each container due to inherit from LifecycleBase, when the container status changes, will call fireLifecycleEvent method, generate LifecycleEvent, the event listener and hand in the container.LifecycleBase fireLifecycleEvent method see the implementation of the code in listing 1.

    Code listing 1

/**

     * Allow sub classes to fire { @link Lifecycle} events.

     *

     * @param type Event type

     * @param data Data associated with event.

     */

     protected void fireLifecycleEvent(String type, Object data) {

     lifecycle.fireLifecycleEvent(type, data);

     }

    The lifecycle are defined as follows:

/**

     * Used to handle firing lifecycle events.

     * TODO: Consider merging LifecycleSupport into this class.

     */

     private LifecycleSupport lifecycle = new LifecycleSupport( this );

    LifecycleSupport fireLifecycleEvent method implementation, see the code in

    listing 2.

    The code in listing 2

/**

     * Notify all lifecycle event listeners that a particular event has

     * occurred for this Container. The default implementation performs

     * this notification synchronously using the calling thread. gja

     *

     * @param type Event type

     * @param data Event data

     */

     public void fireLifecycleEvent(String type, Object data) {

     LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);

     LifecycleListener interested[] = listeners;

     for ( int i = 0; i < interested.length; i++ )

     interested[i].lifecycleEvent(event);

     }

    Code listing 2 the event notification to all monitor the current container lifecycle listener LifecycleListener, and call the LifecycleListener lifecycleEvent method.This a listener cache maintained by the each container, its implementation is as follows:

/**

     * The set of registered LifecycleListeners for event notifications.

     */

     private LifecycleListener listeners[] = new LifecycleListener[0];

    When was the listener in the listeners added?Each container in the new, initialize,start, destroy, be added to the process of the parent container will call the superclass LifecycleBase addLifecycleListener method, addLifecycleListener see the implementation of the code in listing 3.

    The code in listing 3

@Override

     public void addLifecycleListener(LifecycleListener listener) {

     lifecycle.addLifecycleListener(listener);

     }

    See from listing 3, LifecycleBase addLifecycleListener method is actually

    a simple methods to addLifecycleListener LifecycleSupport agent, LifecycleSupport addLifecycleListener method implementation, as shown in

    listing 4.

    The code in listing 4

/**

     * Add a lifecycle event listener to this component.

     *

     * @param listener The listener to add

     */

     public void addLifecycleListener(LifecycleListener listener) {

     synchronized (listenersLock) {

     LifecycleListener results[] =

     new LifecycleListener[listeners.length + 1 ];

     for ( int i = 0; i < listeners.length; i++ )

     results[i] = listeners[i];

     results[listeners.length] = listener;

     listeners = results;

     }

     }

    In listing 2, we talked about the container will eventually call each LifecycleListener lifecycleEvent method of interested in this container, then LifecycleListener lifecycleEvent method will do?For the sake of simplicity, we take the listener AprLifecycleListener, for example, AprLifecycleListener lifecycleEvent method implementation, as shown in listing 5.

    The code in listing 5

/**

     * Primary entry point for startup and shutdown events.

     *

     * @param event The event that has occurred

     */

     public void lifecycleEvent(LifecycleEvent event) {

     if (Lifecycle.INIT_EVENT.equals(event.getType())) {

     synchronized (lock) {

     init();

     if (aprAvailable) {

     try {

     initializeSSL();

     } catch (Throwable t) {

     log.info(sm.getString( "aprListener.sslInit" ));

     }

     }

     }

     } else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {

     synchronized (lock) {

Report this document

For any questions or suggestions please email
cust-service@docsford.com