Detailed explanation of Lifecycle in Tomcat (source code reading)


Original  May 06, 2016 00:06:01
  • 1186

        In tomcat, the life cycle of each component needs to be managed uniformly. Generally, the component that calls the component starts or stops the current component. For example, the connector component controls the start and stop of the processor component. Therefore, the main class of each component will inherit Lifecycle interface. The cycle control in tomcat is designed using the observer pattern. The main classes and interfaces are as follows:

  1. Lifecycle interface (classes that want to use life cycle control will inherit this class)
  2. LifecycleListener interface (listeners all inherit this class)
  3. LifecycleSupport class (used to manage listeners)
  4. LifecycleEvent class (this class is an auxiliary class used as a parameter type)
  5. LifecycleException class (exception class)

Talk is cheap. Show me the code.


Lifecycle interface

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;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

LifecycleListener interface

public interface LifecycleListener {
    public void lifecycleEvent(LifecycleEvent event);
}
  • 1
  • 2
  • 3

LifecycleSupport class 
This class is an intermediate class whose main purpose is to manage the LifecycleListener[] array. and

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

    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

LifecycleEvent class 
This class is an auxiliary class that is used as the parameter type of the lifecycleEvent (LifecycleEvent event) method.

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

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

LifecycleException class 
This class is just an exception class, and the code here is omitted.

Actual call in 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;

    }*/
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324720438&siteId=291194637