Lifecycle details in Tomcat (source reading)


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

In tomcat, every component life cycle needs to be managed uniformly. Generally, the component that invokes 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 inherits the Lifecycle interface. The period control in Tomcat is designed by observer mode. The main classes and interfaces are as follows:

  1. Lifecycle interface (classes to use lifecycle control inherit this class)
  2. LifecycleListener interface (listeners will inherit this class)
  3. LifecycleSupport class (used to manage listeners)
  4. LifecycleEvent class (this class is a helper 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

Lifecycle listener interface

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

LifecycleSupport class
This class is an intermediate class. Its 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
    //Add listener, the method of dynamic array in this method is worth learning
    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 for 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 only an exception class, where the code is omitted.

Actual call in connector

As shown in tomcat4, the default implementation class HttpConnector of the connector component implements the Lifecycle interface, as follows

public final class HttpConnector implements Connector, Lifecycle, Runnable{

//Instantiate a LifecycleSupport class to manage the registration and cancellation of listeners.
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 + "]";
        //Activate the listener, which calls the lifecycleEvent() method of each listener.
        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;

    }*/
}

Tags: Tomcat socket

Posted on Mon, 30 Mar 2020 22:22:56 -0700 by mfalomir