读懂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智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

工厂ERP管理系统实现源码(JAVA)

工厂进销存管理系统是一个集采购管理、仓库管理、生产管理和销售管理于一体的综合解决方案。该系统旨在帮助企业优化流程、提高效率、降低成本,并实时掌握各环节的运营状况。 在采购管理方面,系统能够处理采购订单、供应商管理和采购入库等流程,确保采购过程的透明和高效。仓库管理方面,实现库存的精准管理,包括入库、出库、盘点等操作,确保库存数据的准确性和实时性。 生产管理模块则涵盖了生产计划制定、物料需求计划、

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

kubelet组件的启动流程源码分析

概述 摘要: 本文将总结kubelet的作用以及原理,在有一定基础认识的前提下,通过阅读kubelet源码,对kubelet组件的启动流程进行分析。 正文 kubelet的作用 这里对kubelet的作用做一个简单总结。 节点管理 节点的注册 节点状态更新 容器管理(pod生命周期管理) 监听apiserver的容器事件 容器的创建、删除(CRI) 容器的网络的创建与删除

red5-server源码

red5-server源码:https://github.com/Red5/red5-server

TL-Tomcat中长连接的底层源码原理实现

长连接:浏览器告诉tomcat不要将请求关掉。  如果不是长连接,tomcat响应后会告诉浏览器把这个连接关掉。    tomcat中有一个缓冲区  如果发送大批量数据后 又不处理  那么会堆积缓冲区 后面的请求会越来越慢。

Windows环境利用VS2022编译 libvpx 源码教程

libvpx libvpx 是一个开源的视频编码库,由 WebM 项目开发和维护,专门用于 VP8 和 VP9 视频编码格式的编解码处理。它支持高质量的视频压缩,广泛应用于视频会议、在线教育、视频直播服务等多种场景中。libvpx 的特点包括跨平台兼容性、硬件加速支持以及灵活的接口设计,使其可以轻松集成到各种应用程序中。 libvpx 的安装和配置过程相对简单,用户可以从官方网站下载源代码