Tomcat source code analysis-component startup implementation analysis, tomcat source code analysis component
Tomcat is composed of multiple components. How does Tomcat manage its lifecycle? Here we will analyze the implementation of its lifecycle from the Tomcat source code;
The Bootstrape class is the Tomcat entry. All components can manage the Lifecycle by implementing the Lifecycle interface. When Tomcat is started, you only need to call start () of the Server container (), then, the parent container starts the sub-container in sequence and closes the sub-container.
By reading the source code, we can see that a Server contains one or more services, and a Service contains one iner, one or more ctor ins and iner contain four containers: Engine, Host, Context, and Wrapper;
The startup sequence of Tomcat components is as follows:
StandardServer. start () -- "StandardServer. startInternal () -- "StandardService (). start () -- StandardService. startInternal () --> "StandardEngine (). start () -- "StandardEngine. start other components in startInternal ()-StandardEngine, and close the component;
Now we use the Demo to simulate the startup of Tomcat.
Simulate the Demo UML class diagram
Simulate Demo sequence diagram
The main code segment is as follows:
Catalina class:
Package co. solinx. pattern. observer;/*** Created by LX on 2014/11/26. */public class Catalina {public static void main (String [] args) {// the server that consists of multiple components is the largest StandardServer server in the periphery = new StandardServer ();
// Add the listener server. addLifecycleListener (new ContextConfig () for the server; // Add a service StandardService service = new StandardService (); server. AddService (service );
// Add the listener service. addLifecycleListener (new ContextConfig () for the service; // Add an engine StandardEngine standardEngine = new StandardEngine ();
// Add the listener standardEngine for the engine. addLifecycleListener (new EngineConfig (); StandardHost standardHost = new StandardHost ("localhost"); // StandardHost testHost = new StandardHost ("test"); // standardHost. addLifecycleListener (new EngineConfig (); standardEngine. addChild ("localhost", standardHost); // standardEngine. addChild ("test", testHost); // Add the engine Container service to the service. setContainer (standardEngine); try {server. start ();} catch (LifecycleException e) {e. printStackTrace ();}}}
StandardServer class
package co.solinx.Pattern.Observer;/** * Created by LX on 2014/11/26. */public class StandardServer extends LifecycleBase implements Context { Service services[] = new Service[0]; @Override protected void startInternal() throws LifecycleException { for (int i = 0; i < services.length; i++) { services[i].start(); } System.out.println("StandardServer start"); } public void AddService(Service service) { Service result[] = new Service[services.length + 1]; System.arraycopy(services, 0, result, 0, services.length); result[services.length] = service; services = result; } }
StandardService class:
package co.solinx.Pattern.Observer;/** * Created by LX on 2014/11/26. */public class StandardService extends LifecycleBase implements Service, Context { protected ContainerBase container = null; @Override protected void startInternal() throws LifecycleException { container.start(); System.out.println("StandardService start"); } public void setContainer(ContainerBase container) { this.container = container; }}
StandardEngine class:
package co.solinx.Pattern.Observer;/** * Created by LX on 2014/11/26. */public class StandardEngine extends ContainerBase { @Override protected void startInternal() throws LifecycleException { super.startInternal(); System.out.println("StandardEngine start"); } protected void addChild(String key, Container container) { super.addChild(key, container); }}
LifecycleSupport class:
Package co. solinx. pattern. observer;/*** Created by LX on 2014/11/26. * A specific listener */public class LifecycleSupport {public LifecycleSupport (Lifecycle lifecycle) {super (); this. lifecycle = lifecycle;} private Lifecycle lifecycle = null; private LifecycleListener listeners [] = new LifecycleListener [0]; private final Object listenersLock = new Object (); // Lock object for changes to listeners public Void addLifecycleListener (LifecycleListener listener) {synchronized (listenersLock) {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 [] = listeners; for (int I = 0; I <interested. length; I ++) interested [I]. lifecycleEvent (event);} public void removeLifecycleListener (LifecycleListener listener) {synchronized (listenersLock) {int n =-1; for (int I = 0; I <listeners. length; I ++) {if (listeners [I] = liste Ner) {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 ;}}}
Simulation program running result:
First Article address: Solinx
Http://www.solinx.co/archives/86