Tomcat中Lifecycle詳解(原始碼閱讀)

小鞅發表於2016-05-06

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

    }*/
}
 

相關文章