码迷,mamicode.com
首页 > 其他好文 > 详细

tomcat的事件监听

时间:2015-01-10 20:58:50      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

//事件涉及的三个组件:事件源、事件对象、事件监听器

 

//一个总的事件监听器接口,所有不同分类的事件监听器都继承这个接口

public interface EventListener {
}

//例如 ServletContextListener 这个ServletContext监听器  他有两个方法,一个是初始化方法,另一个是销毁的方法。在web.xml中配置了listener,那么容器在启动时就会扫描web.xml的listener的配置,自动调用contextInitialized 方法,在容器重启时,他会先调用contextDestroyed销毁的方法,在调用contextInitialized 创建的方法。在容器关闭时,会调用contextDestroyed方法。但我刚启动完容器,立刻关闭,是不会调用到contextDestroyed方法的。我想他这里应该有设置时间的吧。

public interface ServletContextListener extends EventListener {
    /**
     ** Notification that the web application initialization
     ** process is starting.
     ** All ServletContextListeners are notified of context
     ** initialization before any filter or servlet in the web
     ** application is initialized.
     */

    public void contextInitialized ( ServletContextEvent sce );  //接受的参数是ServletContextEvent 事件源
/** ** Notification that the servlet context is about to be shut down. ** All servlets and filters have been destroy()ed before any ** ServletContextListeners are notified of context ** destruction. */ 

  public void contextDestroyed ( ServletContextEvent sce ); }

//每一个事件对象都有一个总的事件对象类,下面这个就是了。

public class EventObject implements java.io.Serializable {
    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object  source;

    /**
     * Constructs a prototypical Event.
     *
     * @param    source    The object on which the Event initially occurred.
     * @exception  IllegalArgumentException  if source is null.
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");
     //在这里封装一个事件源
        this.source = source;
    }

    /**
     * The object on which the Event initially occurred.
     *
     * @return   The object on which the Event initially occurred.
     */
    public Object getSource() {
        return source; //得到事件源
    }

    /**
     * Returns a String representation of this EventObject.
     *
     * @return  A a String representation of this EventObject.
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }
}
public class ServletContextEvent extends java.util.EventObject { 

    /** Construct a ServletContextEvent from the given context.
     *
     * @param source - the ServletContext that is sending the event.
     */
    public ServletContextEvent(ServletContext source) {
    super(source);
    }
    
    /**
     * Return the ServletContext that changed.
     *
     * @return the ServletContext that sent the event.
     */
    public ServletContext getServletContext () { 
    return (ServletContext) super.getSource();//返回一个真正的事件源
    }
    
}

这就是tomcat的事件监听设计模型。

tomcat的事件监听

标签:

原文地址:http://www.cnblogs.com/hjy9420/p/4215561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!