读懂tomact源码三:Service

2024-08-24 05:32
文章标签 源码 service 读懂 tomact

本文主要是介绍读懂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 {// ----------------- Propertiespublic 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 Methodspublic 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 listenerssupport.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 listenerssupport.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 listenerssupport.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 firstif (container != null) {synchronized (container) {container.start();}}synchronized (executors) {for (Executor executor : executors) {executor.start();}}// Start our defined Connectors secondsynchronized (connectorsLock) {for (Connector connector : connectors) {try {// If it has already failed, don't try and start itif (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 Executorsfor (Executor executor : findExecutors()) {if (executor instanceof LifecycleMBeanBase) {((LifecycleMBeanBase) executor).setDomain(getDomain());}executor.init();}// Initialize our defined Connectorssynchronized (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 firstif (container != null) {synchronized (container) {container.start();}}synchronized (executors) {for (Executor executor : executors) {executor.start();}}// Start our defined Connectors secondsynchronized (connectorsLock) {for (Connector connector : connectors) {try {// If it has already failed, don't try and start itif (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 firstsynchronized (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 secondif (container != null) {synchronized (container) {container.stop();}}// Now stop the connectorssynchronized (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

这篇关于读懂tomact源码三:Service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1101583

相关文章

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

电脑密码怎么设置? 一文读懂电脑密码的详细指南

《电脑密码怎么设置?一文读懂电脑密码的详细指南》为了保护个人隐私和数据安全,设置电脑密码显得尤为重要,那么,如何在电脑上设置密码呢?详细请看下文介绍... 设置电脑密码是保护个人隐私、数据安全以及系统安全的重要措施,下面以Windows 11系统为例,跟大家分享一下设置电脑密码的具体办php法。Windo

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步