其他分享
首页 > 其他分享> > How tomcat works(深入剖析tomcat)生命周期Lifecycle

How tomcat works(深入剖析tomcat)生命周期Lifecycle

作者:互联网

How Tomcat Works (6)生命周期Lifecycle

总体概述

这一章讲的是tomcat的组件之一,LifeCycle组件,通过这个组件可以统一管理其他组件,可以达到统一启动/关闭组件的目的,那么LifeCycle是如何实现看似如此神奇的功能的呢?

实现了LifeCycle接口的组件,可以触发如下的事件

而事件监听器LifeCycleListener,它监听到这些事件然后做出响应的举动,在这里就可以去统一的开启/关闭容器

主要类以及接口

LifeCycle接口

public interface Lifecycle {
    /**
     * The LifecycleEvent type for the "component start" event.
     */
    //start事件,其实它就是个字符串
    public static final String START_EVENT = "start";
    public static final String BEFORE_START_EVENT = "before_start";
    public static final String AFTER_START_EVENT = "after_start";
    public static final String STOP_EVENT = "stop";
    public static final String BEFORE_STOP_EVENT = "before_stop";
    public static final String AFTER_STOP_EVENT = "after_stop";

    //添加Listener
    public void addLifecycleListener(LifecycleListener listener);
	//返回实现了这个接口的容器所绑定的listener
    public LifecycleListener[] findLifecycleListeners();
	//移除某个listener
    public void removeLifecycleListener(LifecycleListener listener);
	//lifecycle接口最重要的两个方法,组件必须提供这两个方法的实现,以供父组件调用,
    //实现父组件对于子组件的开启/关闭
    public void start() throws LifecycleException;

    public void stop() throws LifecycleException;


}

LifeEvent类

生命周期事件是org.apache.catalina.LifecycleEvent类的实例,其实这个类也很简单,对于上面的String进行了一些包装,添加了一些额外的属性而已,类定义如下

public final class LifecycleEvent
    extends EventObject {
    
    private Object data = null;

    private Lifecycle lifecycle = null;
	//这里的type就对应lifecycle中定义的那6个事件
    private String type = null;
    
    public LifecycleEvent(Lifecycle lifecycle, String type) {

        this(lifecycle, type, null);

    }
    
    public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {

        super(lifecycle);
        this.lifecycle = lifecycle;
        this.type = type;
        this.data = data;

    }

    public Object getData() {

        return (this.data);

    }

    public Lifecycle getLifecycle() {

        return (this.lifecycle);

    }

    public String getType() {

        return (this.type);

    }


}

标签:String,tomcat,EVENT,How,组件,lifecycle,works,type,public
来源: https://www.cnblogs.com/danzZ/p/13998871.html