惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
GbyAI
GbyAI
B
Blog RSS Feed
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
博客园 - 聂微东
F
Fortinet All Blogs
H
Hacker News: Front Page
A
About on SuperTechFans
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
Cisco Talos Blog
Cisco Talos Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
P
Privacy & Cybersecurity Law Blog
N
Netflix TechBlog - Medium
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
美团技术团队
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
Recent Announcements
Recent Announcements
The GitHub Blog
The GitHub Blog
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com

博客园_首页

Plist 二进制格式 Milvus 和 PGVector,哪个更好? OpenClaw 已过时?在 VS Code 中运行 Hermes Agent! 第30篇文章:一个大三计科生的自白 Manim如何在数学公式中完美显示中文? Docker 部署 RocketMQ 5 并发编程核心概念辨析 C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生 CLI 是什么?为什么大厂突然集体卷命令行? 【从0到1构建一个ClaudeAgent】协作-自主Agent UIImageView 设置图片不生效的原因排查 最小二乘问题详解20:无先验约束下的增量式SFM自由网平差 痞子衡嵌入式:大话双核i.MXRT1180之XIP应用里借助MU实现可靠Flash IAP的方法 AI Chat 封装, SemanticKerne.AiProvider.Unified 已发布 Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量 在后台服务中使用 Scoped 服务,为什么总是报错? H200 安装驱动并使用sglang启动模型 wireshark 抓包Trap上报告警内容 我用 AI 辅助开发了一系列小工具(2):图片压缩工具 [A Primer On MC and CC] 2.1 Memory Consistency 1 - 指令重排序和 SC 模型 Oracle数据库SCN推进技术详解与实践指南 玩转控件:封装个带图片的Label控件 Claude Code 4.7 真正该升级的不是模型,而是你的工作流 前端小白一句话,AI 帮我做了个颜值拉满的桌面媒体播放器。当代码不再是门槛,一句话编程就是现实。 5. WorkBuddy: 小龙虾的灵魂三件套,让你的小龙虾不只是工具 SQLite 分片方案实战:三种分片策略的深度对比 告别简陋 UI!一款基于 Fluent Design 和基于 WinUI 的开源免费、现代化的 Avalonia UI 控件库 关于二进制排列组合枚举的总结 AI开发-python-LangGraph框架(3-27-LangGraph从零实现大模型智能决策工作流) ElasticSearch主分片和副本分片概念详解 【002】HTTPS 粗解:证书、TLS 握手与对后端配置的影响 Hermes Agent 一周暴涨五万 Star,但我劝你别急着追 明明连接的是Redis的DB0,为什么能查到DB3的数据? 【从0到1构建一个ClaudeAgent】协作-Agent团队 熟悉电子元器件之后,电子小白下一步该怎么走? MAF快速入门(23)通过C#类定义Skills .NET 高级开发 | 手写一个对象映射框架 FastAPI数据库ORM怎么选?我肝了三个Demo后,终于不再纠结了 mysqldump 参数拾遗:在遗忘与铭记之间 C# .NET 周刊|2026年3月5期 Claude code入门 - 陈彦斌 一文学习入门 ThingsBoard 开源物联网平台 GitHub 热门项目 | 2026年04月16日 如何为GIT设置全局勾子,为每次提交追加信息 Number.isFinite和isFinite与isNaN()和Number.isNaN的区别 PortSwigger SQL注入LAB2 推荐一个测试人必备的Skills,从功能到性能全搞定(附详细实操和安装下载方式) 筑基期:掌握Odoo基础核心知识点02(Odoo XML 开发方式详解) GLM模型这么火,咱们用vllm也咧一个呗! 深入理解 AbortController:从底层原理到跨语言设计哲学 字符串学习笔记 多租户系统框架的基础模块设计和分析设计 Apache SeaTunnel Zeta 为什么能做到“又快又稳”? AI开发-python-LangGraph框架(3-26-LangGraph基本概念及第一个简单样例) Vue 3 组件通信,别只会用 Props 和 Emits 了,这几个狠活儿你得看看 ElasticSearch7.X版本配置密码 用Manim实现动态交点计算--从一个动点问题说起 团结引擎+Addressable+Instant Game打包抖音小游戏 function call 实战:让 LLM 自动判断 pod 异常、调用日志工具并完成故障分析 bubseek —— 让 Agent 的足迹,变成团队的洞察 通过 C# 读取并导出 PDF 书签 如何用 GitHub Actions 实现 Steam 自动化发布 【从0到1构建一个ClaudeAgent】并发-后台任务 .NET 高级开发 | 定制 ASP.NET Core 框架 电子小白:什么是运算放大器(运放) zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线 堆上的ORW HC32F460 USB CDC通信异常:非对齐访问异常排查 20260413-Hyperbridge 攻击事件:发生在默克尔山上的验证绕过 那些喊着AI 要淘汰你的人,正在靠你的焦虑赚大钱! 深度学习进阶(八)Swin Transformer 最小二乘问题详解19:带先验约束的增量式SFM优化与实现 SnapTranslate 3.0 正式发布:全局划词翻译 + 完整英语学习闭环,一站式搞定查词、记词、复习 工作的意义、工作的困难认知再思考 .NET + AI 进阶实战:基于类的技能开发 - 打造可治理的 Agent 能力模块 【从0到1构建一个ClaudeAgent】规划与协调-技能 上周热点回顾(4.6-4.12) 电子小白的工具三件套:面包板、杜邦线、万能板 单表五亿数据的查询优化 | Mysql、StarRocks 2. WorkBuddy:从“我是谁”到“帮我干活” C# 如何减少代码运行时间:7 个实战技巧 基于HelixToolkit.SharpDX 渲染3D模型 - 笺上知微 从零开始的双臂具身VLA起源及现阶段发展综述 - SkyXZ 记对 xonsh shell 的使用, 脚本编写, 迁移及调优 - pluvium27 受够了Vibe Coding的失控?换个起点,让AI事半功倍 从开始配置漏洞环境到漏洞复现流程 - 難しい 关于10年工作经验的程序员对OpenClaw的实战经验分享以及看法 - 虚无境 Any metadata 的内存布局 C# .NET 周刊|2026年3月2期 - InCerry 我帮你测过了,测试圈排名第二的 Skill 依然很牛逼 Skill Discovery | 无监督技能发现的经典工作总结 - MoonOut 上下文工程是什么?过时了么?一文讲明白! - 一枫说码 开了 TUN 模式还是直连?90% 的人都踩过这个坑 AScript扩展多种脚本语言 - rockey627 AI 学习笔记:Agent 的记忆机制 你能被装进一个文件里吗?——7 万人把同事"蒸馏"成了 AI - 我没有三颗心脏 Claude Code 通关手册(七):给 AI 装上技能包——Skills 完全指南 - 暮色之狐 在浏览器中快速编辑代码:VSCode Web 集成实践 - Newbe36524 蒸馏自己 skill?基于 Deepseek 的蒸馏器,丐版蒸馏方式,简单便捷 - To_Carpe_Diem Spring AI Aliababa和AgentScope,哪个更好? - 苏三说技术
Tomcat Request请求处理:Container设计
程序员Seven · 2026-04-30 · via 博客园_首页

理解思路

  • 为什么我们说上面的是Container呢?我们看下几个Container之间的关系

从上图上,我们也可以看出Container顶层也是基于Lifecycle的组件设计的。

  • 在设计Container组件层次组件时,上述4个组件分别做什么的呢?为什么要四种组件呢?

如下是Container接口类的相关注释

 * <li><b>Engine</b> - Representation of the entire Catalina servlet engine,
 *     most likely containing one or more subcontainers that are either Host
 *     or Context implementations, or other custom groups.
 * <li><b>Host</b> - Representation of a virtual host containing a number
 *     of Contexts.
 * <li><b>Context</b> - Representation of a single ServletContext, which will
 *     typically contain one or more Wrappers for the supported servlets.
 * <li><b>Wrapper</b> - Representation of an individual servlet definition
 *     (which may support multiple servlet instances if the servlet itself
 *     implements SingleThreadModel).
 * </ul>

Engine - 表示整个catalina的servlet引擎,多数情况下包含一个或多个子容器,这些子容器要么是Host,要么是Context实现,或者是其他自定义组。

Host - 表示包含多个Context的虚拟主机的。

Context — 表示一个ServletContext,表示一个webapp,它通常包含一个或多个wrapper。

Wrapper - 表示一个servlet定义的(如果servlet本身实现了SingleThreadModel,则可能支持多个servlet实例)。

  • 结合整体的框架图中上述组件部分,我们看下包含了什么

很明显,除了四个组件的嵌套关系,Container中还包含了Realm,Cluster,Listeners, Pipleline等支持组件。

这一点,还可以通过相关注释可以看出:

**Loader** - Class loader to use for integrating new Java classes for this Container into the JVM in which Catalina is running.

**Logger** - Implementation of the log() method signatures of the ServletContext interface.

**Manager** - Manager for the pool of Sessions associated with this Container.

**Realm** - Read-only interface to a security domain, for authenticating user identities and their corresponding roles.

**Resources** - JNDI directory context enabling access to static resources, enabling custom linkages to existing server components when Catalina is embedded in a larger server.

Container的设计

这container应该包含哪些接口呢?如果你看源代码它包含二十多个接口,这里理解的时候一定要分组去理解。

Container的层次结构方法

查找父容器的方法:

/**
  * Get the parent container.
  *
  * @return Return the Container for which this Container is a child, if
  *         there is one. If there is no defined parent, return
  *         <code>null</code>.
  */
public Container getParent();


/**
  * Set the parent Container to which this Container is being added as a
  * child.  This Container may refuse to become attached to the specified
  * Container by throwing an exception.
  *
  * @param container Container to which this Container is being added
  *  as a child
  *
  * @exception IllegalArgumentException if this Container refuses to become
  *  attached to the specified Container
  */
public void setParent(Container container);

由于Engine显然上层是Service,所以里面加了一个getService的方法

/**
  * Return the Service to which this container belongs.
  * @param container The container to start from
  * @return the Service, or null if not found
  */
public static Service getService(Container container) {
    while (container != null && !(container instanceof Engine)) {
        container = container.getParent();
    }
    if (container == null) {
        return null;
    }
    return ((Engine) container).getService();
}

类比树接口,有Parent方法,那肯定也child方法:

/**
  * Add a new child Container to those associated with this Container,
  * if supported.  Prior to adding this Container to the set of children,
  * the child's <code>setParent()</code> method must be called, with this
  * Container as an argument.  This method may thrown an
  * <code>IllegalArgumentException</code> if this Container chooses not
  * to be attached to the specified Container, in which case it is not added
  *
  * @param child New child Container to be added
  *
  * @exception IllegalArgumentException if this exception is thrown by
  *  the <code>setParent()</code> method of the child Container
  * @exception IllegalArgumentException if the new child does not have
  *  a name unique from that of existing children of this Container
  * @exception IllegalStateException if this Container does not support
  *  child Containers
  */
public void addChild(Container child);

/**
  * Obtain the child Containers associated with this Container.
  *
  * @return An array containing all children of this container. If this
  *         Container has no children, a zero-length array is returned.
  */
public Container[] findChildren();

/**
  * Remove an existing child Container from association with this parent
  * Container.
  *
  * @param child Existing child Container to be removed
  */
public void removeChild(Container child);

Container事件监听相关方法

前文我们也分析过Tomcat的事件监听机制,Container也是一样, 比如如下的ContainerListener

/**
  * Add a container event listener to this component.
  *
  * @param listener The listener to add
  */
public void addContainerListener(ContainerListener listener);

/**
  * Obtain the container listeners associated with this Container.
  *
  * @return An array containing the container listeners associated with this
  *         Container. If this Container has no registered container
  *         listeners, a zero-length array is returned.
  */
public ContainerListener[] findContainerListeners();

/**
  * Remove a container event listener from this component.
  *
  * @param listener The listener to remove
  */
public void removeContainerListener(ContainerListener listener);

除了Container级别的,和前文我们理解的一样,还有属性相关的Listener, 显然就增删属性的监听方法

/**
  * Remove a property change listener from this component.
  *
  * @param listener The listener to remove
  */
public void removePropertyChangeListener(PropertyChangeListener listener);

/**
  * Add a property change listener to this component.
  *
  * @param listener The listener to add
  */
public void addPropertyChangeListener(PropertyChangeListener listener);

最后显然还有事件的触发方法

/**
  * Notify all container event listeners that a particular event has
  * occurred for this Container.  The default implementation performs
  * this notification synchronously using the calling thread.
  *
  * @param type Event type
  * @param data Event data
  */
public void fireContainerEvent(String type, Object data);

Container功能支撑方法

前面我们知道,Loader, Logger, Manager, Realm, Resources等支撑功能。这里简单看下接口定义,相关基本实现看下节ContainerBase的实现。

  • Loader
/**
  * Get the parent class loader.
  *
  * @return the parent class loader for this component. If not set, return
  *         {@link #getParent()}.{@link #getParentClassLoader()}. If no
  *         parent has been set, return the system class loader.
  */
public ClassLoader getParentClassLoader();


/**
  * Set the parent class loader for this component. For {@link Context}s
  * this call is meaningful only <strong>before</strong> a Loader has
  * been configured, and the specified value (if non-null) should be
  * passed as an argument to the class loader constructor.
  *
  * @param parent The new parent class loader
  */
public void setParentClassLoader(ClassLoader parent);
  • Logger
/**
  * Obtain the log to which events for this container should be logged.
  *
  * @return The Logger with which this Container is associated.  If there is
  *         no associated Logger, return the Logger associated with the
  *         parent Container (if any); otherwise return <code>null</code>.
  */
public Log getLogger();


/**
  * Return the logger name that the container will use.
  * @return the abbreviated name of this container for logging messages
  */
public String getLogName();
  • Manager

体现在我们之前分析的JMX管理

/**
  * Obtain the JMX name for this container.
  *
  * @return the JMX name associated with this container.
  */
public ObjectName getObjectName();


/**
  * Obtain the JMX domain under which this container will be / has been
  * registered.
  *
  * @return The JMX domain name
  */
public String getDomain();


/**
  * Calculate the key properties string to be added to an object's
  * {@link ObjectName} to indicate that it is associated with this container.
  *
  * @return          A string suitable for appending to the ObjectName
  *
  */
public String getMBeanKeyProperties();

/**
  * Obtain the number of threads available for starting and stopping any
  * children associated with this container. This allows start/stop calls to
  * children to be processed in parallel.
  *
  * @return The currently configured number of threads used to start/stop
  *         children associated with this container
  */
public int getStartStopThreads();
  • Realm
/**
  * Obtain the Realm with which this Container is associated.
  *
  * @return The associated Realm; if there is no associated Realm, the
  *         Realm associated with the parent Container (if any); otherwise
  *         return <code>null</code>.
  */
public Realm getRealm();


/**
  * Set the Realm with which this Container is associated.
  *
  * @param realm The newly associated Realm
  */
public void setRealm(Realm realm);
  • Cluster
/**
  * Get the Cluster for this container.
  *
  * @return The Cluster with which this Container is associated. If there is
  *         no associated Cluster, return the Cluster associated with our
  *         parent Container (if any); otherwise return <code>null</code>.
  */
public Cluster getCluster();


/**
  * Set the Cluster with which this Container is associated.
  *
  * @param cluster the Cluster with which this Container is associated.
  */
public void setCluster(Cluster cluster);
  • 其它
/**
  * Return a name string (suitable for use by humans) that describes this
  * Container.  Within the set of child containers belonging to a particular
  * parent, Container names must be unique.
  *
  * @return The human readable name of this container.
  */
public String getName();


/**
  * Set a name string (suitable for use by humans) that describes this
  * Container.  Within the set of child containers belonging to a particular
  * parent, Container names must be unique.
  *
  * @param name New name of this container
  *
  * @exception IllegalStateException if this Container has already been
  *  added to the children of a parent Container (after which the name
  *  may not be changed)
  */
public void setName(String name);

/**
  * Sets the number of threads available for starting and stopping any
  * children associated with this container. This allows start/stop calls to
  * children to be processed in parallel.
  * @param   startStopThreads    The new number of threads to be used
  */
public void setStartStopThreads(int startStopThreads);


/**
  * Obtain the location of CATALINA_BASE.
  *
  * @return  The location of CATALINA_BASE.
  */
public File getCatalinaBase();


/**
  * Obtain the location of CATALINA_HOME.
  *
  * @return The location of CATALINA_HOME.
  */
public File getCatalinaHome();

Container基本实现:ContainerBase

就讲讲几个比较核心的

Logger

日志记录器,比较简单,直接看代码

/**
  * Return the Logger for this Container.
  */
@Override
public Log getLogger() {
    if (logger != null)
        return logger;
    logger = LogFactory.getLog(getLogName());
    return logger;
}


/**
  * @return the abbreviated name of this container for logging messages
  */
@Override
public String getLogName() {

    if (logName != null) {
        return logName;
    }
    String loggerName = null;
    Container current = this;
    while (current != null) {
        String name = current.getName();
        if ((name == null) || (name.equals(""))) {
            name = "/";
        } else if (name.startsWith("##")) {
            name = "/" + name;
        }
        loggerName = "[" + name + "]"
            + ((loggerName != null) ? ("." + loggerName) : "");
        current = current.getParent();
    }
    logName = ContainerBase.class.getName() + "." + loggerName;
    return logName;

}

Cluster

  • getCluster:读锁,获取子类的cluster,如果没有则返回父类的cluster;
  • getClusterInternal: 读锁,获取子类的cluster
  • setCluster: 写锁,设置container的cluster;由于cluster具备生命周期,所以需要对停止旧的cluster,启动新的cluster;设置成功后,再触发cluster变更事件。
/**
  * The cluster with which this Container is associated.
  */
protected Cluster cluster = null;
private final ReadWriteLock clusterLock = new ReentrantReadWriteLock();

/**
  * The parent Container to which this Container is a child.
  */
protected Container parent = null;

/**
  * Return the Cluster with which this Container is associated.  If there is
  * no associated Cluster, return the Cluster associated with our parent
  * Container (if any); otherwise return <code>null</code>.
  */
@Override
public Cluster getCluster() {
    Lock readLock = clusterLock.readLock();
    readLock.lock();
    try {
        if (cluster != null)
            return cluster;

        if (parent != null)
            return parent.getCluster();

        return null;
    } finally {
        readLock.unlock();
    }
}


/*
  * Provide access to just the cluster component attached to this container.
  */
protected Cluster getClusterInternal() {
    Lock readLock = clusterLock.readLock();
    readLock.lock();
    try {
        return cluster;
    } finally {
        readLock.unlock();
    }
}


/**
  * Set the Cluster with which this Container is associated.
  *
  * @param cluster The newly associated Cluster
  */
@Override
public void setCluster(Cluster cluster) {

    Cluster oldCluster = null;
    Lock writeLock = clusterLock.writeLock();
    writeLock.lock();
    try {
        // Change components if necessary
        oldCluster = this.cluster;
        if (oldCluster == cluster)
            return;
        this.cluster = cluster;

        // Stop the old component if necessary
        if (getState().isAvailable() && (oldCluster != null) &&
            (oldCluster instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldCluster).stop();
            } catch (LifecycleException e) {
                log.error(sm.getString("containerBase.cluster.stop"), e);
            }
        }

        // Start the new component if necessary
        if (cluster != null)
            cluster.setContainer(this);

        if (getState().isAvailable() && (cluster != null) &&
            (cluster instanceof Lifecycle)) {
            try {
                ((Lifecycle) cluster).start();
            } catch (LifecycleException e) {
                log.error(sm.getString("containerBase.cluster.start"), e);
            }
        }
    } finally {
        writeLock.unlock();
    }

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

Realm

Realm和上面的Cluster方法基本一致。

/**
 * Return the Realm with which this Container is associated.  If there is
 * no associated Realm, return the Realm associated with our parent
 * Container (if any); otherwise return <code>null</code>.
 */
@Override
public Realm getRealm() {

    Lock l = realmLock.readLock();
    l.lock();
    try {
        if (realm != null)
            return realm;
        if (parent != null)
            return parent.getRealm();
        return null;
    } finally {
        l.unlock();
    }
}


protected Realm getRealmInternal() {
    Lock l = realmLock.readLock();
    l.lock();
    try {
        return realm;
    } finally {
        l.unlock();
    }
}

/**
 * Set the Realm with which this Container is associated.
 *
 * @param realm The newly associated Realm
 */
@Override
public void setRealm(Realm realm) {

    Lock l = realmLock.writeLock();
    l.lock();
    try {
        // Change components if necessary
        Realm oldRealm = this.realm;
        if (oldRealm == realm)
            return;
        this.realm = realm;

        // Stop the old component if necessary
        if (getState().isAvailable() && (oldRealm != null) &&
            (oldRealm instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldRealm).stop();
            } catch (LifecycleException e) {
                log.error(sm.getString("containerBase.realm.stop"), e);
            }
        }

        // Start the new component if necessary
        if (realm != null)
            realm.setContainer(this);
        if (getState().isAvailable() && (realm != null) &&
            (realm instanceof Lifecycle)) {
            try {
                ((Lifecycle) realm).start();
            } catch (LifecycleException e) {
                log.error(sm.getString("containerBase.realm.start"), e);
            }
        }

        // Report this property change to interested listeners
        support.firePropertyChange("realm", oldRealm, this.realm);
    } finally {
        l.unlock();
    }
}

name等属性

此类属性改变时触发属性变更事件,比如name是容器的名字,name变更会触发name变更事件。

/**
  * The human-readable name of this Container.
  */
protected String name = null;


/**
  * Return a name string (suitable for use by humans) that describes this
  * Container.  Within the set of child containers belonging to a particular
  * parent, Container names must be unique.
  */
@Override
public String getName() {
    return name;
}


/**
  * Set a name string (suitable for use by humans) that describes this
  * Container.  Within the set of child containers belonging to a particular
  * parent, Container names must be unique.
  *
  * @param name New name of this container
  *
  * @exception IllegalStateException if this Container has already been
  *  added to the children of a parent Container (after which the name
  *  may not be changed)
  */
@Override
public void setName(String name) {
    if (name == null) {
        throw new IllegalArgumentException(sm.getString("containerBase.nullName"));
    }
    String oldName = this.name;
    this.name = name;
    support.firePropertyChange("name", oldName, this.name);
}

child相关

添加子容器

/**
  * Add a new child Container to those associated with this Container,
  * if supported.  Prior to adding this Container to the set of children,
  * the child's <code>setParent()</code> method must be called, with this
  * Container as an argument.  This method may thrown an
  * <code>IllegalArgumentException</code> if this Container chooses not
  * to be attached to the specified Container, in which case it is not added
  *
  * @param child New child Container to be added
  *
  * @exception IllegalArgumentException if this exception is thrown by
  *  the <code>setParent()</code> method of the child Container
  * @exception IllegalArgumentException if the new child does not have
  *  a name unique from that of existing children of this Container
  * @exception IllegalStateException if this Container does not support
  *  child Containers
  */
@Override
public void addChild(Container child) {
    if (Globals.IS_SECURITY_ENABLED) {
        PrivilegedAction<Void> dp =
            new PrivilegedAddChild(child);
        AccessController.doPrivileged(dp);
    } else {
        addChildInternal(child);
    }
}

private void addChildInternal(Container child) {

    if (log.isDebugEnabled()) {
        log.debug("Add child " + child + " " + this);
    }

    synchronized(children) {
        if (children.get(child.getName()) != null)
            throw new IllegalArgumentException(
                    sm.getString("containerBase.child.notUnique", child.getName()));
        child.setParent(this);  // May throw IAE 设置父容器
        children.put(child.getName(), child); // 使用map,方便通过name查找子容器
    }

    fireContainerEvent(ADD_CHILD_EVENT, child); // 触发添加子容器的事件

    // Start child // 注意下这里,没有将start方法放到synchronized的原因
    // Don't do this inside sync block - start can be a slow process and
    // locking the children object can cause problems elsewhere
    try {
        if ((getState().isAvailable() ||
                LifecycleState.STARTING_PREP.equals(getState())) &&
                startChildren) {
            child.start();
        }
    } catch (LifecycleException e) {
        throw new IllegalStateException(sm.getString("containerBase.child.start"), e);
    }
}

查找子容器

/**
  * Return the child Container, associated with this Container, with
  * the specified name (if any); otherwise, return <code>null</code>
  *
  * @param name Name of the child Container to be retrieved
  */
@Override
public Container findChild(String name) {
    if (name == null) {
        return null;
    }
    synchronized (children) {
        return children.get(name);
    }
}
/**
  * Return the set of children Containers associated with this Container.
  * If this Container has no children, a zero-length array is returned.
  */
@Override
public Container[] findChildren() {
    synchronized (children) {
        Container results[] = new Container[children.size()];
        return children.values().toArray(results);
    }
}
  • 删除子容器

子容器有生命周期,所以应该是先停止,然后销毁(distroy), 再触发删除事件,最后将children中子容器删除。

/**
  * Remove an existing child Container from association with this parent
  * Container.
  *
  * @param child Existing child Container to be removed
  */
@Override
public void removeChild(Container child) {

    if (child == null) {
        return;
    }

    try {
        if (child.getState().isAvailable()) {
            child.stop();
        }
    } catch (LifecycleException e) {
        log.error(sm.getString("containerBase.child.stop"), e);
    }

    boolean destroy = false;
    try {
        // child.destroy() may have already been called which would have
        // triggered this call. If that is the case, no need to destroy the
        // child again.
        if (!LifecycleState.DESTROYING.equals(child.getState())) {
            child.destroy();
            destroy = true;
        }
    } catch (LifecycleException e) {
        log.error(sm.getString("containerBase.child.destroy"), e);
    }

    if (!destroy) {
        fireContainerEvent(REMOVE_CHILD_EVENT, child);
    }

    synchronized(children) {
        if (children.get(child.getName()) == null)
            return;
        children.remove(child.getName());
    }

}

Lifecycle的模板方法

  • initInternal

startStopThreads 默认为 1 ,所以 reconfigureStartStopExecutor 方法会走 if 语句,而 startStopExecutor 最开始是没有赋值的,startStopExecutor instanceof InlineExecutorService 会返回 false,因此最终会执行 startStopExecutor = new InlineExecutorService(),InlineExecutorService 只是简单地实现了 java.util.concurrent.AbstractExecutorService 类。 最终 reconfigureStartStopExecutor 给 startStopExecutor 这个成员变量设置了,startStopExecutor。

/**
  * The number of threads available to process start and stop events for any
  * children associated with this container.
  */
private int startStopThreads = 1;
protected ExecutorService startStopExecutor;


@Override
protected void initInternal() throws LifecycleException {
    reconfigureStartStopExecutor(getStartStopThreads()); // 设置一个线程池来处理子容器启动和关闭事件
    super.initInternal(); // 调用LifecycleMBeanBase的方法
}


private void reconfigureStartStopExecutor(int threads) {
    if (threads == 1) {
        // Use a fake executor
        if (!(startStopExecutor instanceof InlineExecutorService)) {
            startStopExecutor = new InlineExecutorService(); // 执行这里
        }
    } else {
        // Delegate utility execution to the Service
        Server server = Container.getService(this).getServer();
        server.setUtilityThreads(threads);
        startStopExecutor = server.getUtilityExecutor();
    }
}
  • startInternal

试想,container中有很多组件,而且属于Lifecycle生命周期管理;那么启动容器的时候,必然是逐个将这些子组件(包括子容器)启动起来。

/**
  * Start this component and implement the requirements
  * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
  *
  * @exception LifecycleException if this component detects a fatal error
  *  that prevents this component from being used
  */
@Override
protected synchronized void startInternal() throws LifecycleException {

    // Start our subordinate components, if any
    logger = null;
    getLogger();
    Cluster cluster = getClusterInternal();
    if (cluster instanceof Lifecycle) {
        ((Lifecycle) cluster).start();
    }
    Realm realm = getRealmInternal();
    if (realm instanceof Lifecycle) {
        ((Lifecycle) realm).start();
    }

    // Start our child containers, if any
    Container children[] = findChildren();
    List<Future<Void>> results = new ArrayList<>();
    for (Container child : children) {
        results.add(startStopExecutor.submit(new StartChild(child)));
    }

    MultiThrowable multiThrowable = null; // 引入一个MultiThrowable,来收集多个异常

    for (Future<Void> result : results) {
        try {
            result.get();
        } catch (Throwable e) {
            log.error(sm.getString("containerBase.threadedStartFailed"), e);
            if (multiThrowable == null) {
                multiThrowable = new MultiThrowable();
            }
            multiThrowable.add(e);
        }

    }
    if (multiThrowable != null) {
        throw new LifecycleException(sm.getString("containerBase.threadedStartFailed"),
                multiThrowable.getThrowable());
    }

    // Start the Valves in our pipeline (including the basic), if any
    if (pipeline instanceof Lifecycle) {
        ((Lifecycle) pipeline).start();
    }

    setState(LifecycleState.STARTING);

    // 看这个,本质是调用最上层server的utilityExecutorWrapper 线程池去执行 ContainerBackgroundProcessorMonitor 任务
    if (backgroundProcessorDelay > 0) {
        monitorFuture = Container.getService(ContainerBase.this).getServer()
                .getUtilityExecutor().scheduleWithFixedDelay(
                        new ContainerBackgroundProcessorMonitor(), 0, 60, TimeUnit.SECONDS);
    }
}
  • stopInternal

和initInternal初始化子组件方式倒过来,逐一停止子组件,并触发相关事件。

/**
  * Stop this component and implement the requirements
  * of {@link org.apache.catalina.util.LifecycleBase#stopInternal()}.
  *
  * @exception LifecycleException if this component detects a fatal error
  *  that prevents this component from being used
  */
@Override
protected synchronized void stopInternal() throws LifecycleException {

    // Stop our thread
    if (monitorFuture != null) {
        monitorFuture.cancel(true);
        monitorFuture = null;
    }
    threadStop();

    setState(LifecycleState.STOPPING);

    // Stop the Valves in our pipeline (including the basic), if any
    if (pipeline instanceof Lifecycle &&
            ((Lifecycle) pipeline).getState().isAvailable()) {
        ((Lifecycle) pipeline).stop();
    }

    // Stop our child containers, if any
    Container children[] = findChildren();
    List<Future<Void>> results = new ArrayList<>();
    for (Container child : children) {
        results.add(startStopExecutor.submit(new StopChild(child)));
    }

    boolean fail = false;
    for (Future<Void> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString("containerBase.threadedStopFailed"), e);
            fail = true;
        }
    }
    if (fail) {
        throw new LifecycleException(
                sm.getString("containerBase.threadedStopFailed"));
    }

    // Stop our subordinate components, if any
    Realm realm = getRealmInternal();
    if (realm instanceof Lifecycle) {
        ((Lifecycle) realm).stop();
    }
    Cluster cluster = getClusterInternal();
    if (cluster instanceof Lifecycle) {
        ((Lifecycle) cluster).stop();
    }
}
  • destroyInternal

对比下initInternal,它初始化了什么就destory什么

@Override
protected void destroyInternal() throws LifecycleException {

    Realm realm = getRealmInternal();
    if (realm instanceof Lifecycle) {
        ((Lifecycle) realm).destroy();
    }
    Cluster cluster = getClusterInternal();
    if (cluster instanceof Lifecycle) {
        ((Lifecycle) cluster).destroy();
    }

    // Stop the Valves in our pipeline (including the basic), if any
    if (pipeline instanceof Lifecycle) {
        ((Lifecycle) pipeline).destroy();
    }

    // Remove children now this container is being destroyed
    for (Container child : findChildren()) {
        removeChild(child);
    }

    // Required if the child is destroyed directly.
    if (parent != null) {
        parent.removeChild(this);
    }

    // If init fails, this may be null
    if (startStopExecutor != null) {
        startStopExecutor.shutdownNow();
    }

    super.destroyInternal(); // 调用LifecycleMBeanBase的方法
}