Tomcat学习笔记(十三)

 

服务器组件和服务组件 


服务器组件 
org.apache.catalina.Server接口的实例表示Catalina的整个servlet引擎,包含了所有的组件。使用一种优雅的方法来启动/关闭整个系统,不需要再对连接器和容器分别启动/关闭。 
关闭/启动的工作原理。当启动服务器组件时,他会启动其中所有的组件,然后它就一直等待关闭命令,如果要关闭系统,可以向指定端口发送一条关闭命令。服务器收到关闭命令后,就会关闭其中所有的组件。 
看一下Server接口的几个方法。

public interface Server extends Lifecycle {
    public int getPort();//返回监听关闭的端口
    public void setPort(int port);//设置关闭端口
    public Catalina getCatalina();//返回catalina组件
    public void addService(Service service);//添加服务
    public Service findService(String name);//查询服务
    public Service[] findServices();//查询所有服务
    public void removeService(Service service);//移除服务
}

StandardServer类 
StandardServer类是Server接口的标准实现。 
一个服务器组件可以有0个或者多个服务组件。StandardServer类提供了addService()方法、removeService()方法和findService方法的实现。 
addService将服务组件添加到服务器组件中,服务器组件中包含一个数组来保存服务组件。

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);
        }

    }

removeService从服务组件数组中移除,并停止服务。

public void removeService(Service service) {

        synchronized (services) {
            int j = -1;
            for (int i = 0; i < services.length; i++) {
                if (service == services[i]) {
                    j = i;
                    break;
                }
            }
            if (j < 0)
                return;
            try {
                services[j].stop();
            } catch (LifecycleException e) {
                // Ignore
            }
            int k = 0;
            Service results[] = new Service[services.length - 1];
            for (int i = 0; i < services.length; i++) {
                if (i != j)
                    results[k++] = services[i];
            }
            services = results;

            // Report this property change to interested listeners
            support.firePropertyChange("service", service, null);
        }
    }

StandardServer类的4个生命周期相关的方法,是由LifecycleBase中的init调用initInternal(StandardServer),start调用startInternal(StandardServer),stop调用stopInternal(StandardServer),await(StandardServer)方法。 
initInternal主要是对资源的初始化和服务组件的初始化。

protected void initInternal() throws LifecycleException {
        super.initInternal();
        ...
        // Register the naming resources
        globalNamingResources.init();
        ...
        // Initialize our defined Services
        for (int i = 0; i < services.length; i++) {
            services[i].init();
        }
    }

startInternal设置生命周期,设置服务器组件的状态,启动服务组件。

@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();
            }
        }
    }

await一直等待停止命令。 
Service接口 
主要与连接器,服务器组件,执行器相关的信息

public interface Service extends Lifecycle {
     public void addConnector(Connector connector);
     public Connector[] findConnectors();
     public void removeConnector(Connector connector);
     public void addExecutor(Executor ex);
     public Executor[] findExecutors();
     public Executor getExecutor(String name);
     public void removeExecutor(Executor ex);
     public Server getServer();
     public void setServer(Server server);
}

ServiceStandard对Service的接口实现,同时也有生命周期方法 
startInternal

protected void startInternal() throws LifecycleException {

        if(log.isInfoEnabled())
            log.info(sm.getString("standardService.start.name", this.name));
        setState(LifecycleState.STARTING);

        // Start our defined Container first
        if (container != null) {
            synchronized (container) {
                container.start();
            }
        }

        synchronized (executors) {
            for (Executor executor: executors) {
                executor.start();
            }
        }

        // Start our defined Connectors second
        synchronized (connectors) {
            for (Connector connector: connectors) {
                try {
                    // If it has already failed, don't try and start it
                    if (connector.getState() != LifecycleState.FAILED) {
                        connector.start();
                    }
                } catch (Exception e) {
                    log.error(sm.getString(
                            "standardService.connector.startFailed",
                            connector), e);
                }
            }
        }
    }

initInternal初始化容器,执行器以及连接器。

 protected void initInternal() throws LifecycleException {

        super.initInternal();

        if (container != null) {
            container.init();
        }

        // Initialize any Executors
        for (Executor executor : findExecutors()) {
            if (executor instanceof LifecycleMBeanBase) {
                ((LifecycleMBeanBase) executor).setDomain(getDomain());
            }
            executor.init();
        }

        // Initialize our defined Connectors
        synchronized (connectors) {
            for (Connector connector : connectors) {
                try {
                    connector.init();
                } catch (Exception e) {
                    String message = sm.getString(
                            "standardService.connector.initFailed", connector);
                    log.error(message, e);

                    if (Boolean.getBoolean("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE"))
                        throw new LifecycleException(message);
                }
            }
        }
    }

stopInternal停止连接器和执行器。

protected void stopInternal() throws LifecycleException {

        // Pause connectors first
        synchronized (connectors) {
            for (Connector connector: connectors) {
                try {
                    connector.pause();
                } catch (Exception e) {
                    log.error(sm.getString(
                            "standardService.connector.pauseFailed",
                            connector), e);
                }
            }
        }

        if(log.isInfoEnabled())
            log.info(sm.getString("standardService.stop.name", this.name));
        setState(LifecycleState.STOPPING);

        // Stop our defined Container second
        if (container != null) {
            synchronized (container) {
                container.stop();
            }
        }

        // Now stop the connectors
        synchronized (connectors) {
            for (Connector connector: connectors) {
                if (!LifecycleState.STARTED.equals(
                        connector.getState())) {
                    // Connectors only need stopping if they are currently
                    // started. They may have failed to start or may have been
                    // stopped (e.g. via a JMX call)
                    continue;
                }
                try {
                    connector.stop();
                } catch (Exception e) {
                    log.error(sm.getString(
                            "standardService.connector.stopFailed",
                            connector), e);
                }
            }
        }

        synchronized (executors) {
            for (Executor executor: executors) {
                executor.stop();
            }
        }
    }

 

posted @ 2017-07-12 00:38  lzyer  阅读(246)  评论(0编辑  收藏  举报