首页
登录 | 注册

Tomcat中Lifecycle详解(源码阅读)

        在tomcat中,每一个组件生命周期都是需要统一管理的,一般是由调用该组件的组件来启动或停止当前组建,如connector组件控制processor组件启动和停止,因此每个组件主要类都会继承Lifecycle接口。tomcat中周期控制采用观察者模式来设计。其中主要类和接口如下:

  1. Lifecycle接口(要使用生命周期控制的类都会继承该类)
  2. LifecycleListener接口(监听器都会继承该类)
  3. LifecycleSupport类(用来对监听器进行管理)
  4. LifecycleEvent类(该类是一个辅助类,用来作为参数类型)
  5. LifecycleException类(异常类)

Talk is cheap. Show me the code.


Lifecycle接口

public interface Lifecycle {

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

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

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

    public void addLifecycleListener(LifecycleListener listener);

    public LifecycleListener[] findLifecycleListeners();

    public void removeLifecycleListener(LifecycleListener listener);

    public void start() throws LifecycleException;

    public void stop() throws LifecycleException;

}

LifecycleListener接口

public interface LifecycleListener {
    public void lifecycleEvent(LifecycleEvent event);
}

LifecycleSupport类
该类是一种中间类,主要目的就是对LifecycleListener[]数组进行管理。而且

public final class LifecycleSupport {
    // ----------------------------------------------------------- Constructors    
    public LifecycleSupport(Lifecycle lifecycle) {

        super();
        this.lifecycle = lifecycle;

    }
    // ----------------------------------------------------- Instance Variables
    private Lifecycle lifecycle = null;
    private LifecycleListener listeners[] = new LifecycleListener[0];
    // --------------------------------------------------------- Public Methods
    //添加监听器,该方法中动态数组的方法值得借鉴
    public void addLifecycleListener(LifecycleListener listener) {

      synchronized (listeners) {
          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[] = null;
        synchronized (listeners) {
            interested = (LifecycleListener[]) listeners.clone();
        }
        for (int i = 0; i < interested.length; i++)
            interested[i].lifecycleEvent(event);

    }
    public void removeLifecycleListener(LifecycleListener listener) {

        synchronized (listeners) {
            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;
        }

    }


}

LifecycleEvent类
该类是一个辅助类,用来 lifecycleEvent(LifecycleEvent event)方法的参数类型。

public final class LifecycleEvent extends EventObject {

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

    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;

    }

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

    private Object data = null;

    private Lifecycle lifecycle = null;

    private String type = null;

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

    public Object getData() {

        return (this.data);

    }

    public Lifecycle getLifecycle() {

        return (this.lifecycle);

    }

    public String getType() {

        return (this.type);

    }

}

LifecycleException类
该类只是一个异常类,该处代码省略。

connector中的实际调用

如tomcat4中connector组件的默认实现类HttpConnector就实现了Lifecycle接口,如下所示


public final class HttpConnector implements Connector, Lifecycle, Runnable{

//实例化一个LifecycleSupport 类,用来管理监听器的注册和取消。
protected LifecycleSupport lifecycle = new LifecycleSupport(this);

public void addLifecycleListener(LifecycleListener listener) {

        lifecycle.addLifecycleListener(listener);

    }
    public LifecycleListener[] findLifecycleListeners() {
        return lifecycle.findLifecycleListeners();
    }
    public void removeLifecycleListener(LifecycleListener listener) {

        lifecycle.removeLifecycleListener(listener);
    }
public void start() throws LifecycleException {

        // Validate and update our current state
        if (started)
            throw new LifecycleException
                (sm.getString("httpConnector.alreadyStarted"));
        threadName = "HttpConnector[" + port + "]";
        //激活监听器,该方法会调用各个监听器的lifecycleEvent()方法。
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;

/*        // Start our background thread
        threadStart();

        // Create the specified minimum number of processors
        while (curProcessors < minProcessors) {
            if ((maxProcessors > 0) && (curProcessors >= maxProcessors))
                break;
            HttpProcessor processor = newProcessor();
            recycle(processor);
        }*/

    }

    public void stop() throws LifecycleException {

        // Validate and update our current state
        if (!started)
            throw new LifecycleException
                (sm.getString("httpConnector.notStarted"));
        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
        started = false;

        // Gracefully shut down all processors we have created
        for (int i = created.size() - 1; i >= 0; i--) {
            HttpProcessor processor = (HttpProcessor) created.elementAt(i);
            if (processor instanceof Lifecycle) {
                try {
                    ((Lifecycle) processor).stop();
                } catch (LifecycleException e) {
                    log("HttpConnector.stop", e);
                }
            }
        }

      /*  synchronized (threadSync) {
            // Close the server socket we were using
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    ;
                }
            }
            // Stop our background thread
            threadStop();
        }
        serverSocket = null;

    }*/
}


2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号