Tomcat Source Analysis-component Start implementation analysis

Source: Internet
Author: User

Tomcat is made up of multiple components, so how does Tomcat manage their lifecycle, from the Tomcat source to analyze the implementation of its life cycle;

The Bootstrape class is the portal for Tomcat, and all the components are sufficient to manage the lifecycle by implementing the Lifecycle interface, and Tomcat starts by invoking the server container's start (), and then the parent container launches the child containers that he contains, which is closed.

Read the source to know that a server contains one or more service, a service contains a container, one or more connector, container also contains the engine, Host, Context, Wrapper of four containers;

?

Tomcat's component boot order is:

Standardserver.start ()--"standardserver.startinternal ()--" Standardservice (). Start ()-- Standardservice.startinternal ()--standardengine (). Start ()--"standardengine.startinternal ()-" Other components are started in the Standardengine, and the components are closed.

Now we're simply simulating the Tomcat launch with demo

Simulation Demo UML Class diagram

Analog Demo Timing Diagram

The main code snippet is as follows:

Catalina class:

Package co.solinx.pattern.observer;/** * Created to LX on 2014/11/26. */public class Catalina {public static void main (string[] args) {//tomcat A server consisting of multiple components is the outermost largest standa Rdserver Server = new Standardserver ();
Add Listener Server.addlifecyclelistener (new Contextconfig ()) to the server; Add a service Standardservice service = new Standardservice (); Server. AddService (service);
Add Listener Service.addlifecyclelistener (new Contextconfig ()) to the service; Add an engine standardengine standardengine = new Standardengine ();
Add Listener Standardengine.addlifecyclelistener (new Engineconfig ()) to the engine; Standardhost standardhost = new Standardhost ("localhost");//Standardhost testhost = new Standardhost ("test");// Standardhost.addlifecyclelistener (New Engineconfig ()); Standardengine.addchild ("localhost", standardhost);//Standardengine.addchild ("Test", testhost); Add Engine container Service.setcontainer (standardengine) to service; try {server.start (); } catch (Lifecycleexception e) {e.printstacktrace (); } }}

Standardserver class

Package co.solinx.pattern.observer;/** * Created to 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 to 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 to 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 to LX on 2014/11/26.        * Agent for 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) {Synchroni  Zed (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) {in            T 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; }    }}

Simulation Program Run Results:

Article starting address:solinx

Http://www.solinx.co/archives/86

Tomcat Source Analysis-component Start implementation analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.