tomcat&生命周期&事件监听器

本文介绍了Java中的事件监听器机制,包括事件对象、事件源和事件监听器的三者关系,并通过具体示例阐述了其工作原理。接着,文章深入探讨了Tomcat如何利用这种机制来管理组件的生命周期,展示了Tomcat中的事件对象、事件源和监听器代码片段,揭示了在启动组件时如何触发相关组件生命周期的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:java中的事件监听器

   要实现java的监听器需要实现以下三个对象:

   1事件对象:一般继承自java.util.EventObject,会作为参数用于监听处理方法中,要根据监听事件的需求自定义事件对象。类似mvc模式中的model实体类,主要用来存放事件处理需要的相关参数。

  2事件源:事件源用于触发监听器方法,同时也可维护监听事件队列(启动、增加和删除监听器)。

  3事件监听:以事件对象为参数,执行具体的事件方法。

  三者关系如下:

具体例子如下:

//
public  class  DemoEvent  extends  EventObject{
     public  DemoEvent( Object  source){
         super( source);
    }
}
//Listener
public  interface  DemoEventListener {
     //
     public  void  processEvent( DemoEvent  demoEvent);//将事件对象作为参数传入方法
}
//Listener实现接口方法
public  class  FirstListener  implements  DemoEventListener{
     public  void  processEvent( DemoEvent  demoEvent){
         System. out. println( "FirstListener");
    }
}
public  class  SecondListener  implements  DemoEventListener{
     public  void  processEvent( DemoEvent  demoEvent){
         System. out. println( "SecondListener");
    }
}
//
public  class  DemoEventSource{
     private  List < DemoEventListener >  listeners  =  new  ArrayList < DemoEventListener >();//维护监听器数组
    
     public  DemoEventListener(){
        
    }
    
     public  void  addListener( DemoEventListener  demoEventListener){
         listeners. add( demoEventListener);
    }
    
     public  void  notifyEvent(){
         for( DemoEventListener  demoEventListener :  listeners){//遍历监听器数组,并调用每一个监听器的方法
              DemoEvent  demoEvent  =  new  DemoEvent();
              demoEventListener. processEvent( demoEvent);
        }
    }
}
//
public  class  Demo {
     /**
     *  @param  args
     */
     public  static  void  main( String[]  args) {
         // TODO Auto-generated method stub
         //
         DemoEventSource  demoEventSource  =  new  DemoEventSource();
         //
         FirstListener  firstListener  =  new  FirstListener();
         DemoEventSource. addListener( firstListener);
         //
         SecondListener  secondListener = new  SecondListener();
         DemoEventSource. addListener( secondListener);
         //
         DemoEventSource. notifyDemoEvent();
    }
}

二:tomcat中的生命周期

  tomcat中当启动一个组件时往往也需要启动一系列的相关组件,即每个组件都有自己的生命周期。tomcat就利用上述的监听器机制实现了对组件生命周期的管理。

  首先,事件对象的定义如下:

import java.util.EventObject;


/**
 * General event for notifying listeners of significant changes on a component
 * that implements the Lifecycle interface.  In particular, this will be useful
 * on Containers, where these events replace the ContextInterceptor concept in
 * Tomcat 3.x.
 *
 * @author Craig R. McClanahan
 * @version $Revision: 1.3 $ $Date: 2001/07/22 20:13:30 $
 */

public final class LifecycleEvent
    extends EventObject {


    // ----------------------------------------------------------- Constructors


    /**
     * Construct a new LifecycleEvent with the specified parameters.
     *
     * @param lifecycle Component on which this event occurred
     * @param type Event type (required)
     */
    public LifecycleEvent(Lifecycle lifecycle, String type) {

        this(lifecycle, type, null);

    }


    /**
     * Construct a new LifecycleEvent with the specified parameters.
     *
     * @param lifecycle Component on which this event occurred
     * @param type Event type (required)
     * @param data Event data (if any)
     */
    public LifecycleEvent(Lifecycle lifecycle, String type, Object data) {

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

    }


    // ----------------------------------------------------- Instance Variables


    /**
     * The event data associated with this event.
     */
    private Object data = null;


    /**
     * The Lifecycle on which this event occurred.
     */
    private Lifecycle lifecycle = null;


    /**
     * The event type this instance represents.
     */
    private String type = null;


    // ------------------------------------------------------------- Properties


    /**
     * Return the event data of this event.
     */
    public Object getData() {

        return (this.data);

    }


    /**
     * Return the Lifecycle on which this event occurred.
     */
    public Lifecycle getLifecycle() {

        return (this.lifecycle);

    }


    /**
     * Return the event type of this event.
     */
    public String getType() {

        return (this.type);

    }


}
这里只是简单的定义了data、lifecycle和type三个成员变量。

事件源的主要代码如下:

public final class LifecycleSupport {

    /**
     * The source component for lifecycle events that we will fire.
     */
    private Lifecycle lifecycle = null;


    /**
     * The set of registered LifecycleListeners for event notifications.
     */
    private LifecycleListener listeners[] = new LifecycleListener[0];


    /**
     * 触发某个事件
     * 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);//调用实现lifecycle接口的类中的事件运行方法

    }


}
这里忽略构造函数以及对listeners[]的相关增删操作,fireLifecycleEvent通过参数生成事件对象并传给监听器处理。

监听器代码如下:

public class SimpleContextLifecycleListener implements LifecycleListener {

  public void lifecycleEvent(LifecycleEvent event) {
    Lifecycle lifecycle = event.getLifecycle();//从事件对象中取得lifecycle中的值
    System.out.println("SimpleContextLifecycleListener's event " +
      event.getType().toString());
    if (Lifecycle.START_EVENT.equals(event.getType())) {
      System.out.println("Starting context.");
    }
    else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
      System.out.println("Stopping context.");
    }
  }
}
这样一个监听器就完成了,怎么使用呢?直接调用定义一个事件源对象protected LifecycleSupport lifecycle = new LifecycleSupport();然后通过调用lifecycle.fireLifecycleEvent(...,...);就可以运行监听器事件了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值