交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
第2 章: 生命周期的管理
分享
未结
0
1020
李延
LV6
2021-08-02
悬赏:20积分
# 生命状态 目前一共定义了 11种不同的什么状态,如下 新建 NEW(false, null), 初始化 INITIALIZING(false, Lifecycle.BEFORE_INIT_EVENT), 初始化后 INITIALIZED(false, Lifecycle.AFTER_INIT_EVENT), 启动前 STARTING_PREP(false, Lifecycle.BEFORE_START_EVENT), 启动中 TARTING(true, Lifecycle.START_EVENT), 启动完成 STARTED(true, Lifecycle.AFTER_START_EVENT), 停止前 STOPPING_PREP(true, Lifecycle.BEFORE_STOP_EVENT), 停止中 STOPPING(false, Lifecycle.STOP_EVENT), 停止后 STOPPED(false, Lifecycle.AFTER_STOP_EVENT), 销毁中 DESTROYING(false, Lifecycle.BEFORE_DESTROY_EVENT), 销毁后 DESTROYED(false, Lifecycle.AFTER_DESTROY_EVENT), 失败 FAILED(false, null); # Lifecycle 该接口统一管理tomcat 的什么周期。其中 - 定义了生命周期的状态 - 关于监听器的3个方法 增,删,查 - 4个生命周期方法 :init 、start 、stop、destroy # LifecycleBase 实现了Lifecycle 的方法。下文主要对 4个生命周期方法进行说明。 整体来说,这个一个模版方法。分别预留一些方法交给子类实现,分别是: - 初始化时:protected abstract void initInternal() throws LifecycleException; - 启动容器:protected abstract void startInternal() throws LifecycleException; - 停止时: protected abstract void startInternal() throws LifecycleException; - 销毁时: protected abstract void startInternal() throws LifecycleException; ## init ```java @Override public final synchronized void init() throws LifecycleException { //首先判断只有处于NEW状态,才可以初始化。 if (!state.equals(LifecycleState.NEW)) { invalidTransition(Lifecycle.BEFORE_INIT_EVENT); } try { //设置状态为初始化中 setStateInternal(LifecycleState.INITIALIZING, null, false); //初始化,子类实现 initInternal(); //设置状态为初始化后 setStateInternal(LifecycleState.INITIALIZED, null, false); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException( sm.getString("lifecycleBase.initFail",toString()), t); } } ``` ## start public final synchronized void start() throws LifecycleException { //TODO 已经处于启动中的3个状态能再启动,但不会报错 if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) || LifecycleState.STARTED.equals(state)) { if (log.isDebugEnabled()) { Exception e = new LifecycleException(); log.debug(sm.getString("lifecycleBase.alreadyStarted", toString()), e); } else if (log.isInfoEnabled()) { log.info(sm.getString("lifecycleBase.alreadyStarted", toString())); } return; } //新建的对象,先执行初始化 if (state.equals(LifecycleState.NEW)) { init(); //失败的对象,先停止 } else if (state.equals(LifecycleState.FAILED)) { stop(); //其他不在停止或初始化完成的对象,抛错 } else if (!state.equals(LifecycleState.INITIALIZED) && !state.equals(LifecycleState.STOPPED)) { invalidTransition(Lifecycle.BEFORE_START_EVENT); } try { //设置为启动前 setStateInternal(LifecycleState.STARTING_PREP, null, false); //开始启动 startInternal(); //失败的结果,将容器停止 if (state.equals(LifecycleState.FAILED)) { // This is a 'controlled' failure. The component put itself into the // FAILED state so call stop() to complete the clean-up. stop(); //其他不是启动中的结果,抛错 } else if (!state.equals(LifecycleState.STARTING)) { // Shouldn't be necessary but acts as a check that sub-classes are // doing what they are supposed to. invalidTransition(Lifecycle.AFTER_START_EVENT); // 设置状态为 启动结束 } else { setStateInternal(LifecycleState.STARTED, null, false); } } catch (Throwable t) { // This is an 'uncontrolled' failure so put the component into the // FAILED state and throw an exception. ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException(sm.getString("lifecycleBase.startFail", toString()), t); } } ## stop ```java @Override public final synchronized void stop() throws LifecycleException { //已经处于启动中的3个状态不能停止 if (LifecycleState.STOPPING_PREP.equals(state) || LifecycleState.STOPPING.equals(state) || LifecycleState.STOPPED.equals(state)) { if (log.isDebugEnabled()) { Exception e = new LifecycleException(); log.debug(sm.getString("lifecycleBase.alreadyStopped", toString()), e); } else if (log.isInfoEnabled()) { log.info(sm.getString("lifecycleBase.alreadyStopped", toString())); } return; } //NEW状态直接设置为停止状态 if (state.equals(LifecycleState.NEW)) { state = LifecycleState.STOPPED; return; } //其他不在启动结束 和失败状态 直接抛错 if (!state.equals(LifecycleState.STARTED) && !state.equals(LifecycleState.FAILED)) { invalidTransition(Lifecycle.BEFORE_STOP_EVENT); } try { if (state.equals(LifecycleState.FAILED)) { // Don't transition to STOPPING_PREP as that would briefly mark the // component as available but do ensure the BEFORE_STOP_EVENT is // fired fireLifecycleEvent(BEFORE_STOP_EVENT, null); } else { setStateInternal(LifecycleState.STOPPING_PREP, null, false); } stopInternal(); // Shouldn't be necessary but acts as a check that sub-classes are // doing what they are supposed to. if (!state.equals(LifecycleState.STOPPING) && !state.equals(LifecycleState.FAILED)) { invalidTransition(Lifecycle.AFTER_STOP_EVENT); } setStateInternal(LifecycleState.STOPPED, null, false); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException(sm.getString("lifecycleBase.stopFail",toString()), t); } finally { if (this instanceof Lifecycle.SingleUse) { // Complete stop process first setStateInternal(LifecycleState.STOPPED, null, false); destroy(); } } } ```
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号