读懂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

相关文章

Spring Boot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)

《SpringBoot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)》本文将以一个实际案例(用户管理系统)为例,详细解析SpringBoot中Co... 目录引言:为什么学习Spring Boot分层架构?第一部分:Spring Boot的整体架构1.1

java 恺撒加密/解密实现原理(附带源码)

《java恺撒加密/解密实现原理(附带源码)》本文介绍Java实现恺撒加密与解密,通过固定位移量对字母进行循环替换,保留大小写及非字母字符,由于其实现简单、易于理解,恺撒加密常被用作学习加密算法的入... 目录Java 恺撒加密/解密实现1. 项目背景与介绍2. 相关知识2.1 恺撒加密算法原理2.2 Ja

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思