读懂tomact源码三:Service

service是一个接口,在源码上有一段这样的话:
A Service is a group of one or more Connectors that share a single Container to process their incoming requests.  This arrangement allows, for example,a non-SSL and SSL connector to share the same population of web apps.A given JVM can contain any number of Service instances; however, they are completely independent of each other and share only the basic JVM facilities and classes on the system class path.
 Service是一组连接器共享单个容器,来解析他们收到的请求。这些请求可以是ssl或者不是ssl的。一个给定的jvm包含任何数量的service实例;但是,它们彼此相互独立,共享一份jvm虚拟机和系统变量的类。

Service的源码

public interface Service extends Lifecycle {
    // ----------------- Properties
    public Container getContainer();
    public void setContainer(Container container);
    public String getInfo();
    public String getName();
    public void setName(String name);
    public Server getServer();
    public void setServer(Server server);
    public ClassLoader getParentClassLoader();
    public void setParentClassLoader(ClassLoader parent);

    // ----------------- Public Methods
    public void addConnector(Connector connector);
    public void removeConnector(Connector connector);
    public void addExecutor(Executor ex);
    public Executor[] findExecutors();
    public Executor getExecutor(String name);
    public void removeExecutor(Executor ex);
}

StandardService实现了Service这个接口

Property

  1. private final Object connectorsLock = new Object();
    connectors的锁,主要是add、remove、restart操作
  2. protected PropertyChangeSupport support = new PropertyChangeSupport(this);
    PropertyChangeSupport,我也不懂是什么鬼,看着是支持一些属性的更改
  3. protected Connector connectors[] = new Connector[0];
    这个servce所关联的Connector
  4. protected Container container = null;
    The Container associated with this Service
  5. private Server server = null;
    这个service的所有者吧

method

  1. setContainer : 设置service的Container
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);

    }
  1. addConnector,添加Connector
  public void addConnector(Connector connector) {

        synchronized (connectorsLock) {
            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);
        }

    }
  1. removeConnector:删去一个Connector
public void removeConnector(Connector connector) {

        synchronized (connectorsLock) {
            int j = -1;
            for (int i = 0; i < connectors.length; i++) {
                if (connector == connectors[i]) {
                    j = i;
                    break;
                }
            }
            if (j < 0)
                return;
            if (connectors[j].getState().isAvailable()) {
                try {
                    connectors[j].stop();
                } catch (LifecycleException e) {
                    log.error(sm.getString(
                            "standardService.connector.stopFailed",
                            connectors[j]), e);
                }
            }
            connector.setService(null);
            int k = 0;
            Connector results[] = new Connector[connectors.length - 1];
            for (int i = 0; i < connectors.length; i++) {
                if (i != j)
                    results[k++] = connectors[i];
            }
            connectors = results;

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

    }
  1. 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 (connectorsLock) {
            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);
                }
            }
        }
    }

Start nested components : Executors, Connectors and Containers and implement the requirements of org.apache.catalina.util.LifecycleBase#startInternal().

5:initInternal:主要是初始化,Executor、Connector、container
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 (connectorsLock) {
        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);
            }
        }
    }
}

6:startInternal:Executor、Connector、container开始执行

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

7、stopInternal

protected void stopInternal() throws LifecycleException {

    // Pause connectors first
    synchronized (connectorsLock) {
        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 (connectorsLock) {
        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();
        }
    }
}

**

note:注意Internal中Executor、Connector、container的次序

**

8.getParentClassLoader

  public ClassLoader getParentClassLoader() {
        if (parentClassLoader != null)
            return (parentClassLoader);
        if (server != null) {
            return (server.getParentClassLoader());
        }
        return (ClassLoader.getSystemClassLoader());
    }

如果没有parentClassLoader和server,返回SystemClassLoader

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值