应用启动和关闭监听器功能分析

一、功能概述

ApplicationStartAndStopListener 是一个 Spring Boot 应用启动和关闭监听器,用于记录系统的启动和关闭时间到数据库,并实现优雅停机机制。

二、使用背景

在系统运行过程中,需要记录系统的启动和关闭时间,以便进行:

  • 系统运行时间统计
  • 系统稳定性监控
  • 故障排查和日志分析
  • 优雅停机,确保资源正确释放

三、核心实现

3.1 类定义

java 复制代码
@Slf4j
@Component
public class ApplicationStartAndStopListener implements ApplicationRunner {
    private final SysStartLogService sysStartLogService;
    private final ConfigurableApplicationContext context;
    private final boolean startLogEnabled;
}

实现接口: ApplicationRunner - 在应用启动后执行

依赖注入:

  • SysStartLogService: 系统启动日志服务
  • ConfigurableApplicationContext: Spring 应用上下文
  • startLogEnabled: 是否启用启动日志(配置项)

3.2 应用启动处理

java 复制代码
@Override
public void run(ApplicationArguments args) {
    if (!startLogEnabled) {
        return;
    }
    try {
        Date startTime = new Date();
        log.info("启动时间:" + DateUtil.format(startTime, DatePattern.NORM_DATETIME_PATTERN));
        
        // 获取数据库中 id 最大的一条数据
        SysStartLog sysStartLog = sysStartLogService.getOne(
            Wrappers.<SysStartLog>lambdaQuery()
                .orderByDesc(SysStartLog::getId)
                .last("limit 1")
        );
        
        // 更新启动时间
        if (sysStartLog != null) {
            sysStartLog.setStartTime(startTime);
            sysStartLogService.updateById(sysStartLog);
        }
    } catch (Exception e) {
        log.warn("添加到系统启动日志表失败!" + e.getMessage());
    }
    
    shutdownHook(context);
}

处理流程:

复制代码
应用启动
    ↓
检查 startLogEnabled 配置
    ↓
获取当前启动时间
    ↓
查询数据库中最新的一条启动日志
    ↓
更新启动时间
    ↓
注册关闭钩子

3.3 应用关闭处理

java 复制代码
private void shutdownHook(ConfigurableApplicationContext run) {
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        Date endTime = new Date();
        System.out.println("开始执行优雅停机逻辑...");
        log.info("停机时间:" + DateUtil.format(endTime, DatePattern.NORM_DATETIME_PATTERN));
        
        // 添加到系统启动日志表
        SysStartLog sysStartLog = new SysStartLog();
        sysStartLog.setEndTime(endTime);
        sysStartLogService.saveOrUpdate(sysStartLog);
        
        // 延迟5秒关闭Spring Boot应用上下文
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.schedule(run::close, 5, TimeUnit.SECONDS);
        executor.shutdown();
    }));
}

优雅停机流程:

复制代码
JVM 收到关闭信号
    ↓
触发 ShutdownHook
    ↓
记录停机时间
    ↓
保存到数据库
    ↓
延迟5秒
    ↓
关闭 Spring 应用上下文

四、调用方式

4.1 自动触发

该监听器通过 @Component 注解自动注册到 Spring 容器,在应用启动时自动执行。

4.2 配置启用

在配置文件中设置:

properties 复制代码
# 是否启用启动日志
startLog.enabled=true

六、技术要点

6.1 ApplicationRunner 接口

java 复制代码
public interface ApplicationRunner {
    void run(ApplicationArguments args) throws Exception;
}

特点:

  • 在应用启动完成后执行
  • 可以访问启动参数
  • 执行顺序:CommandLineRunnerApplicationRunner

6.2 ShutdownHook

java 复制代码
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    // 清理逻辑
}));

特点:

  • 在 JVM 关闭前执行
  • 用于资源清理
  • 不能保证一定执行(如强制 kill)

6.3 优雅停机

java 复制代码
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(run::close, 5, TimeUnit.SECONDS);

作用:

  • 延迟关闭,确保业务处理完成
  • 给予系统缓冲时间
  • 避免数据丢失

七、总结

ApplicationStartAndStopListener 实现了以下功能:

  1. 启动时间记录:应用启动时记录启动时间到数据库
  2. 关闭时间记录:应用关闭时记录关闭时间到数据库
  3. 优雅停机:延迟5秒关闭,确保资源正确释放
  4. 配置控制:通过配置项控制是否启用日志记录
  5. 异常容错:异常不影响应用正常启动

适用场景:

  • 系统运行时间统计
  • 系统稳定性监控
  • 故障排查和日志分析
  • 资源清理和优雅停机
相关推荐
雪的季节6 分钟前
Python 线程同步与异步编程 全解析
java·开发语言
吃饱了得干活17 分钟前
缓存与数据库一致性:从理论到实战
java·后端·面试
520拼好饭被践踏25 分钟前
JAVA+Agent学习day22
java·开发语言·后端·学习
笨蛋不要掉眼泪36 分钟前
Java虚拟机:堆的参数配置
java·开发语言·jvm
霸道流氓气质38 分钟前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
花生了什么事o44 分钟前
DDD 分层架构:六层分层架构
java·架构·ddd
Y3815326621 小时前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存
qq_150841991 小时前
SQL的insert和update二合一指令
java·数据库·sql
2601_964702891 小时前
Claude Opus 5 API 开发实战:对话、文本生成与结构化输出
java·服务器·前端
Sayuanni%31 小时前
Spring IOC
java·spring·rpc