tomcat 7 源码分析-8 生命周期lifecycle和监听listener

tomcat 7 源码分析-8 生命周期lifecycle和监听listener

 

每个应用都有生命周期lifecycle,可能包括init,start,stop,destroy等更多。针对生命周期的变化,如何做变化作出反 应,tomcat在设计的时候,把时间监听listener结合起来,所以listener取的名字是lifecyclelistener,对 lifecycle进行监听。

总的最底层的来自两个interface的设计Lifecycle和LifecycleListener。

lifecycle这个接口定义了除本身生命周期的函数,另外还定义了整个生命周期阶段的事件类型(这个肯定是为 lifecyclelistener准备的),同时还对LifecycleListener有增加,删除,通知等的函数。 LifecycleListener这个接口定义了对事件Event的反应。简单看下Lifecycle和LifecycleListener的代码片 段。

Java代码   收藏代码
  1. public   interface  Lifecycle {  
  2.   
  3.     /**  
  4.      * The LifecycleEvent type for the "component init" event.  
  5.      */   
  6.     public   static   final  String INIT_EVENT =  "init" ;  
  7.   
  8.   
  9.     /**  
  10.      * The LifecycleEvent type for the "component start" event.  
  11.      */   
  12.     public   static   final  String START_EVENT =  "start" ;  
  13.   
  14.   
  15.     /**  
  16.      * The LifecycleEvent type for the "component before start" event.  
  17.      */   
  18.     public   static   final  String BEFORE_START_EVENT =  "before_start" ;  
  19.   
  20.     [..........]  
  21.       
  22.     // --------------------------------------------------------- Public Methods   
  23.   
  24.   
  25.     /**  
  26.      * Add a LifecycleEvent listener to this component.  
  27.      *  
  28.      * @param listener The listener to add  
  29.      */   
  30.     public   void  addLifecycleListener(LifecycleListener listener);  
  31.   
  32.   
  33.     /**  
  34.      * Get the lifecycle listeners associated with this lifecycle. If this   
  35.      * Lifecycle has no listeners registered, a zero-length array is returned.  
  36.      */   
  37.     public  LifecycleListener[] findLifecycleListeners();  
  38.   
  39.     [.............]  
  40.   
  41.     public   void  init()  throws  LifecycleException;  
  42.   
  43.     public   void  start()  throws  LifecycleException;  
  44.   
  45.     public   void  stop()  throws  LifecycleException;  
  46.   
  47.     public   void  destroy()  throws  LifecycleException;  
  48.   
  49.     public  LifecycleState getState();  
  50. }  
public interface Lifecycle {

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


    /**
     * 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";

    [..........]
    
    // --------------------------------------------------------- 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();

    [.............]

    public void init() throws LifecycleException;

    public void start() throws LifecycleException;

    public void stop() throws LifecycleException;

    public void destroy() throws LifecycleException;

    public LifecycleState getState();
}
 
Java代码   收藏代码
  1. public   interface  LifecycleListener {  
  2.   
  3.   
  4.     /**  
  5.      * Acknowledge the occurrence of the specified event.  
  6.      *  
  7.      * @param event LifecycleEvent that has occurred  
  8.      */   
  9.     public   void  lifecycleEvent(LifecycleEvent event);  
  10.   
  11.   
  12. }  
public interface LifecycleListener {


    /**
     * Acknowledge the occurrence of the specified event.
     *
     * @param event LifecycleEvent that has occurred
     */
    public void lifecycleEvent(LifecycleEvent event);


}

 后续的问题是,谁来维护LifecycleListener?谁维护LifecycleListener,必然要提供真正意思上对操作(增,删, 查和通知)函数。Tomcat将这些交给了LifecycleSupport。同时LifecycleSupport还提供对lifecycle传来的 bype,data封装成

LifecycleEvent类,给LifecycleListener用。

Java代码   收藏代码
  1. public   final   class  LifecycleSupport {  
  2.     public  LifecycleSupport(Lifecycle lifecycle) {  
  3.   
  4.         super ();  
  5.         this .lifecycle = lifecycle;  
  6.   
  7.     }  
  8.     private  Lifecycle lifecycle =  null ;  
  9.     private  LifecycleListener listeners[] =  new  LifecycleListener[ 0 ];  
  10.       
  11.     private   final  Object listenersLock =  new  Object();  // Lock object for changes to listeners   
  12.   
  13.     public   void  addLifecycleListener(LifecycleListener listener) {  
  14.   
  15.       synchronized  (listenersLock) {  
  16.           LifecycleListener results[] =  
  17.             new  LifecycleListener[listeners.length +  1 ];  
  18.           for  ( int  i =  0 ; i < listeners.length; i++)  
  19.               results[i] = listeners[i];  
  20.           results[listeners.length] = listener;  
  21.           listeners = results;  
  22.       }  
  23.   
  24.     }  
  25.     public  LifecycleListener[] findLifecycleListeners() {  
  26.   
  27.         return  listeners;  
  28.   
  29.     }  
  30.   
  31.     public   void  fireLifecycleEvent(String type, Object data) {  
  32.   
  33.         LifecycleEvent event = new  LifecycleEvent(lifecycle, type, data);  
  34.         LifecycleListener interested[] = listeners;  
  35.         for  ( int  i =  0 ; i < interested.length; i++)  
  36.             interested[i].lifecycleEvent(event);  
  37.   
  38.     }  
  39.   
  40.     public   void  removeLifecycleListener(LifecycleListener listener) {  
  41.   
  42.         synchronized  (listenersLock) {  
  43.             int  n = - 1 ;  
  44.             for  ( int  i =  0 ; i < listeners.length; i++) {  
  45.                 if  (listeners[i] == listener) {  
  46.                     n = i;  
  47.                     break ;  
  48.                 }  
  49.             }  
  50.             if  (n <  0 )  
  51.                 return ;  
  52.             LifecycleListener results[] =  
  53.               new  LifecycleListener[listeners.length -  1 ];  
  54.             int  j =  0 ;  
  55.             for  ( int  i =  0 ; i < listeners.length; i++) {  
  56.                 if  (i != n)  
  57.                     results[j++] = listeners[i];  
  58.             }  
  59.             listeners = results;  
  60.         }  
  61.   
  62.     }  
  63.   
  64.   
  65. }  
public final class LifecycleSupport {
    public LifecycleSupport(Lifecycle lifecycle) {

        super();
        this.lifecycle = lifecycle;

    }
    private Lifecycle lifecycle = null;
    private LifecycleListener listeners[] = new LifecycleListener[0];
    
    private final Object listenersLock = new Object(); // Lock object for changes to listeners

    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;
      }

    }
    public LifecycleListener[] findLifecycleListeners() {

        return listeners;

    }

    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);

    }

    public void removeLifecycleListener(LifecycleListener listener) {

        synchronized (listenersLock) {
            int n = -1;
            for (int i = 0; i < listeners.length; i++) {
                if (listeners[i] == listener) {
                    n = i;
                    break;
                }
            }
            if (n < 0)
                return;
            LifecycleListener results[] =
              new LifecycleListener[listeners.length - 1];
            int j = 0;
            for (int i = 0; i < listeners.length; i++) {
                if (i != n)
                    results[j++] = listeners[i];
            }
            listeners = results;
        }

    }


}

tomcat的应用都继承之Lifecycle,包括server,service,Connector,GlobalNamingResources等,而针对这些又实现不同的LifecycleListener。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值