Tomcat的生命周期管理

今天总结Tomcat的LifeCycle机制。首先先说容器们都实现了的Lifecycle接口。这个接口来控制容器的生命周期。首先,它定义了六个生命周期的状态,BEFORE_START_EVENT,AFTER_START_EVENT,START_EVENT,STOP_EVENT,BEFORE_STOP_EVENT,AFTER_STOP_EVENT来管理容器的生命周期。而且它里面定义了两个很重要的方法start和stop,用于启动容器和关闭容器时候用。剩下的三个方法是关于listener的addLifecycleListener,findLifecycleListeners, removeLifecycleListener。在tomcat启动的时候,首先先用addLifecycleListener,把相应的listener加进去。
容器们都实现了Lifecycle接口,那么下一步是要给他们加Listener。。。加什么样的Listener呢?LifecycleListener出现啦。listener要实现这个接口才可以哦。这个接口里面只定义了一个方法lifecycleEvent(LifecycleEvent event)。这个方法就是用来写在触发了listener之后要实现什么功能的。那么如何触发listener呢。。。就一个类出现了LifecycleSupport。我们要在容器中实例下这个。然后调用它的fireLifecycleEvent(String type, Object data)方法。它会对找出来容器相关的listener把它们clone一下,然后遍历它们,执行lifecycleEvent方法。这里还要说下LifecycleEvent,这个类表示一个生命周期事件,也是具体存储事件的类。里面有三个属性Lifecycle lifecycle, String type, Object data,分别表示这个事件所属的lifecycle,它的类型,还有要传递的data.


粘代码啦:

这个是LifeCycle:

public interface Lifecycle {


    // ----------------------------------------------------- Manifest Constants


    /**
     * The LifecycleEvent type for the "component start" event.
     */
    public static final String START_EVENT = "start";


    /**
     * The LifecycleEvent type for the "component before start" event.
     */
    public static final String BEFORE_START_EVENT = "before_start";


    /**
     * The LifecycleEvent type for the "component after start" event.
     */
    public static final String AFTER_START_EVENT = "after_start";


    /**
     * The LifecycleEvent type for the "component stop" event.
     */
    public static final String STOP_EVENT = "stop";


    /**
     * The LifecycleEvent type for the "component before stop" event.
     */
    public static final String BEFORE_STOP_EVENT = "before_stop";


    /**
     * The LifecycleEvent type for the "component after stop" event.
     */
    public static final String AFTER_STOP_EVENT = "after_stop";


    // --------------------------------------------------------- Public Methods


    /**
     * Add a LifecycleEvent listener to this component.
     *
     * @param listener The listener to add
     */
    public void addLifecycleListener(LifecycleListener listener);


    /**
     * Get the lifecycle listeners associated with this lifecycle. If this 
     * Lifecycle has no listeners registered, a zero-length array is returned.
     */
    public LifecycleListener[] findLifecycleListeners();


    /**
     * Remove a LifecycleEvent listener from this component.
     *
     * @param listener The listener to remove
     */
    public void removeLifecycleListener(LifecycleListener listener);


    /**
     * Prepare for the beginning of active use of the public methods of this
     * component.  This method should be called before any of the public
     * methods of this component are utilized.  It should also send a
     * LifecycleEvent of type START_EVENT to any registered listeners.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    public void start() throws LifecycleException;


    /**
     * Gracefully terminate the active use of the public methods of this
     * component.  This method should be the last one called on a given
     * instance of this component.  It should also send a LifecycleEvent
     * of type STOP_EVENT to any registered listeners.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that needs to be reported
     */
    public void stop() throws LifecycleException;


}

这个是LifecycleSupport的fireLifecycleEvent方法

public final class LifecycleSupport {
	...
	 /**
     * 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.
     *
     * @param type Event type
     * @param data Event data
     */
    public void fireLifecycleEvent(String type, Object data) {

        LifecycleEvent event = new LifecycleEvent(lifecycle, type, data);
        LifecycleListener interested[] = null;
        synchronized (listeners) {
            interested = (LifecycleListener[]) listeners.clone();
        }
        for (int i = 0; i < interested.length; i++)
            interested[i].lifecycleEvent(event);

    }
	
	...
}


接着再说下容器们的start和stop方法吧。如何做到Tomcat启动的时候只要启动container就可以启动下面的子容器,而不需要一一去start。简单的说,就是父容器把子容器都放在一个叫children的属性中。父容器在start方法里,遍历children list,调用了所有子容器的start方法。我们举个栗子,比如context和wrapper。我们知道context表示一个应用,wrapper是servlet的包装类。这个是context的start方法:

public synchronized void start() throws LifecycleException {
		if (started)
			throw new LifecycleException("SimpleContext has already started");

		// Notify our interested LifecycleListeners
		lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
		started = true;
		try {
			// Start our subordinate components, if any
			if ((loader != null) && (loader instanceof Lifecycle))
				((Lifecycle) loader).start();

			// Start our child containers, if any
			Container children[] = findChildren();
			for (int i = 0; i < children.length; i++) {
				if (children[i] instanceof Lifecycle)
					((Lifecycle) children[i]).start();
			}

			// Start the Valves in our pipeline (including the basic),
			// if any
			if (pipeline instanceof Lifecycle)
				((Lifecycle) pipeline).start();
			// Notify our interested LifecycleListeners
			lifecycle.fireLifecycleEvent(START_EVENT, null);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Notify our interested LifecycleListeners
		lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
	}

参考:

how tomcat works

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值