zoukankan      html  css  js  c++  java
  • Tomcat

    在本机之前做项目的时候,都是使用的MyEcplise+Tomcat工具;但是对Tomcat仅仅是浅浅的了解,后来项目使用Jboss了,兴趣使然,想对Tomcat和JBoss进行一个更加清晰的学习。


    Tomcat介绍

    Tomcat:由Apache组织提供的一种Web服务器,提供对jsp和Servlet的支持。它是一种轻量级的javaWeb容器(服务器),也是当前应用最广的JavaWeb服务器(免费)。

    其他web服务器简介:
    Jboss:是一个遵从JavaEE规范的、开放源代码的、纯Java的EJB服务器,它支持所有的JavaEE规范(免费)。
    GlassFish: 由Oracle公司开发的一款JavaWeb服务器,是一款强健的商业服务器,达到产品级质量(应用很少,收费)。
    Resin:是CAUCHO公司的产品,是一个非常流行的应用服务器,对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JAVA语言开发(收费,应用比较多)。
    WebLogic:是Oracle公司的产品,是目前应用最广泛的Web服务器,支持JavaEE规范,而且不断的完善以适应新的开发要求,适合大型项目(收费,用的不多,适合大公司)。

    Tomcat的系统架构

    Tomcat的系统架构

    Tomcat的核心组件就两个Connector和Container,一个Connector+一个Container构成一个Service,Service就是对外提供服务的组件,有了Service组件Tomcat就可以对外提供服务了,但是光有服务还不行,还得有环境让你提供服务才行,所以最外层的Server就为Service提供了生存的土壤。

    Connector是一个连接器,主要负责接收请求并把请求交给Container,Container就是一个容器,主要装的是具体处理请求的组件。Service主要是为了关联Container与Connector,一个单独的Container或者一个单独的Connector都不能完整处理一个请求,只有两个结合在一起才能完成一个请求的处理。Server这是负责管理Service集合,从图中我们看到一个Tomcat可以提供多种服务,那么这些Serice就是由Server来管理的,具体的工作包括:对外提供一个接口访问Service,对内维护Service集合,维护Service集合又包括管理Service的生命周期、寻找一个请求的Service、结束一个Service等。

    Server

    Server是管理Service接口的,Server是Tomcat的顶级容器,是一个接口,Server接口的标准实现类是StandardServer类,在Server接口中有许多方法,我们重点关注两个方法:addService()和findService(String)。我们先来看看Server接口的全貌:
    Server

    接着看看addService()和findService(String)的实现代码:

    /**
     * Add a new Service to the set of defined Services.
     *
     * @param service The Service to be added
     */
    @Override
    public void addService(Service service) {
    
        service.setServer(this);
    
        synchronized (services) {
            Service results[] = new Service[services.length + 1];
            System.arraycopy(services, 0, results, 0, services.length);
            results[services.length] = service;
            services = results;
    
            if (getState().isAvailable()) {
                try {
                    service.start();
                } catch (LifecycleException e) {
                    // Ignore
                }
            }
    
            // Report this property change to interested listeners
            support.firePropertyChange("service", null, service);
        }
    
    }

    Server使用一个数组来管理Service的,每添加一个Service就把原来的Service拷贝到一个新的数组中,再把新的Service放入Service数组中。所以Server与Service是关联在一起的,那么后面的getState().isAvailable()是干嘛的呢?判断状态是否无效,从而决定是否执行service方法。这里说到了状态,就不得不说Tomcat管理各组件生命周期的Lifecycle接口了:

    Lifecycle接口

    Tomcat中的组件都交给这个接口管理,但是具体组件的生命周期是由包含组件的父容器来管理的,Tomcat中顶级容器管理着Service的生命周期,Service容器又是Connector和Container的父容器,所以这两个组件的生命周期是由Service管理的,Container也有子容器,所以管理着这些子容器的生命周期。这样,只要所有组件都实现了Lifecycle接口,从顶层容器Server开始,就可以控制所有容器的生命周期了。Lifecycle接口中定义了很多状态,在api中详细说明了调用不同方法后的状态转变,同时定义了不同的方法,这些方法在执行后状态会发生相应的改变,在Lifecycle接口中定义了如下方法:
    Lifecycle

    在StandServer中实现了startInernal()方法,就是循环启动StandServer管理的Service的过程,Tomcat的Service都实现了Lifecycle接口,所以被管理的Service都将被通知到,从而执行start()方法,startIntenal()方法是这样的:

    /**
     * Start nested components ({@link Service}s) and implement the requirements
     * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    @Override
    protected void startInternal() throws LifecycleException {
    
        fireLifecycleEvent(CONFIGURE_START_EVENT, null);
        setState(LifecycleState.STARTING);
    
        globalNamingResources.start();
    
        // Start our defined Services
        synchronized (services) {
            for (int i = 0; i < services.length; i++) {
                services[i].start();
            }
        }
    }

    现在所有的Service就会收到通知继而执行start方法。如果一个Service不允许被使用将会抛出一个LifecycleException异常。

    stopIntenal()会通知所有Service执行stop方法,具体处理流程与startIntenal()方法类似。这个执行过程涉及一个非常重要的设计模式,就是观察者模式。

    容器通过Lifecycle接口管理容器的生命周期,那么在父容器的状态改变具体是怎么样通知给子容器的呢?我们注意到有一个fireLifecycleEvent()方法,fireLifecycleEvent()的执行流程如下:

    1. 调用LifecycleBase的fireLifecycleEvent(LifecycleListener listener)方法,LifecycleBase是一个抽象类,实现了Lifecycle接口
    2. 继续调用LifecycleSupport(是一个辅助完成对已经注册监听器的事件通知类,不可被继承,使用final)的fireLifecycleEvent(String type, Object data)方法
    3. 完成事件通知

    fireLifecycleEvent(String type, Object data)的方法如下:

    /**
     * Notify all lifecycle event listeners that a particular event has
     * occurred for this Container.  The default implementation performs
     * this notification synchronously using the calling thread.
     *
     * @param type Event type
     * @param data Event data
     */
    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);
    
    }

    所以,具体事件的通知是由LifecycleListener接口的lifecycleEvent方法完成的,各实现类可以根据不同的情况实现不同的事件监听逻辑.

    Service

    Service是具体提供服务的接口,一个Service包装了Connector和一个Container。
    Service是一个接口,其标准实现类是StandardService:
    Service

    StandardService

    Connector和Container最紧密的方法:setContainer()和addConnector()方法,先看一下setContainer()方法的源码:

    /**
     * Set the <code>Container</code> that handles requests for all
     * <code>Connectors</code> associated with this Service.
     *
     * @param container The new Container
     */
    @Override
    public void setContainer(Container container) {
    
        Container oldContainer = this.container;
        if ((oldContainer != null) && (oldContainer instanceof Engine))
            ((Engine) oldContainer).setService(null);
        this.container = container;
        if ((this.container != null) && (this.container instanceof Engine))
            ((Engine) this.container).setService(this);
        if (getState().isAvailable() && (this.container != null)) {
            try {
                this.container.start();
            } catch (LifecycleException e) {
                // Ignore
            }
        }
        if (getState().isAvailable() && (oldContainer != null)) {
            try {
                oldContainer.stop();
            } catch (LifecycleException e) {
                // Ignore
            }
        }
    
        // Report this property change to interested listeners
        support.firePropertyChange("container", oldContainer, this.container);
    
    }

    从代码中可以看到这个方法主要的任务是设置一个Container容器来处理一个或者多个Connector传送过来的请求。首先判断当前的Service是否已经关联了Container容器,如果已经关联了就去除这个关联关系。如果原来的Container容器已经启动了就终止其生命周期,结束运行并设置新的关联关系,这个新的Container容器开始新的生命周期。最后把这个过程通知给感兴趣的事件监听程序。

    下面看看addConnector的方法:

    /**
     * Add a new Connector to the set of defined Connectors, and associate it
     * with this Service's Container.
     *
     * @param connector The Connector to be added
     */
    @Override
    public void addConnector(Connector connector) {
    
        synchronized (connectors) {
            connector.setService(this);
            Connector results[] = new Connector[connectors.length + 1];
            System.arraycopy(connectors, 0, results, 0, connectors.length);
            results[connectors.length] = connector;
            connectors = results;
    
            if (getState().isAvailable()) {
                try {
                    connector.start();
                } catch (LifecycleException e) {
                    log.error(sm.getString(
                            "standardService.connector.startFailed",
                            connector), e);
                }
            }
    
            // Report this property change to interested listeners
            support.firePropertyChange("connector", null, connector);
        }
    
    }

    执行过程也比较清楚:用一个同步代码块包住connectors数组,首先设置connector与container和service的关联关系,然后让connector开始新的生命周期,最后通知感兴趣的事件监听程序。注意到Connector的管理和Server管理Service一样都使用了数组拷贝并把新的数组赋给当前的数组,从而间接实现了动态数组。



    转载自博主:rhwayfunn

  • 相关阅读:
    2013第38周日Java文件上传下载收集思考
    2013年第38周六这一刻,行动
    2013年9月20日突然的焦虑
    2013中秋
    2013第38周三
    2013年第38周二
    2013第38周一电话开会邮件
    for循环中一个不容小觑的问题
    NPOI 创建Excel,数据读取与写入
    linux下mysql数据的导出和导入
  • 原文地址:https://www.cnblogs.com/aixing/p/13327571.html
Copyright © 2011-2022 走看看