国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Tomcat源碼分析之—組件啟動實現分析

2019-11-14 22:02:25
字體:
來源:轉載
供稿:網友
Tomcat源碼分析之—組件啟動實現分析

Tomcat由多個組件組成,那么Tomcat是怎么對他們的生命周期進行管理的么,這里將從Tomcat源碼去分析其生命周期的實現;

Bootstrape類為Tomcat的入口,所有的組件夠通過實現Lifecycle接口來管理生命周期,Tomcat啟動的時候只需調用Server容器的start(),然后父容器依序啟動他所包含的子容器,關閉也是如此。

通過閱讀源碼可知一個Server里包含一個或多個Service,一個Service里包含一個Container,一個或多個Connector、Container又包含了Engine、Host、Context、Wrapper四個容器;

?

Tomcat的組件啟動順序為:

StandardServer.start()——》StandardServer.startInternal()——》StandardService().start()——StandardService.startInternal() ——>》StandardEngine().start()——》StandardEngine.startInternal()—》StandardEngine中啟動其他組件,組件關閉也是如此;

現在我們通過Demo簡單模擬Tomcat的啟動

模擬Demo UML類圖

模擬Demo時序圖

主要代碼段如下:

Catalina類:

package co.solinx.Pattern.Observer;/** * Created by LX on 2014/11/26. */public class Catalina {    public static void main(String[] args) {        //Tomcat為多個組件組成的server為最外圍最大的一個        StandardServer server = new StandardServer();        //為server添加監聽器        server.addLifecycleListener(new ContextConfig());        //添加一個service        StandardService service = new StandardService();        server.AddService(service);       //為service添加監聽器        service.addLifecycleListener(new ContextConfig());        //添加一個engine        StandardEngine standardEngine = new StandardEngine();        //為engine添加監聽器        standardEngine.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);        //往service添加engine容器        service.setContainer(standardEngine);        try {            server.start();        } catch (LifecycleException e) {            e.PRintStackTrace();        }    }}

StandardServer類

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類:

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類:

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類:

package co.solinx.Pattern.Observer;/** * Created by LX on 2014/11/26. * 代理了具體監聽者 */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] == 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;        }    }}

模擬程序運行結果:

文章首發地址:Solinx

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太仆寺旗| 论坛| 临沧市| 洞头县| 巧家县| 辽中县| 普定县| 兴国县| 平邑县| 乐昌市| 永登县| 建宁县| 景宁| 政和县| 青浦区| 彭阳县| 重庆市| 教育| 莱州市| 班玛县| 垦利县| 石嘴山市| 合肥市| 英山县| 左贡县| 上虞市| 睢宁县| 梅河口市| 永州市| 穆棱市| 龙井市| 临高县| 崇明县| 娱乐| 延长县| 响水县| 旌德县| 枣阳市| 金沙县| 封丘县| 赤水市|