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

一、功能概述

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. 异常容错:异常不影响应用正常启动

适用场景:

  • 系统运行时间统计
  • 系统稳定性监控
  • 故障排查和日志分析
  • 资源清理和优雅停机
相关推荐
linweidong1 小时前
Java 后端开发面试 50 个高频易混淆知识点详解
java·spring boot·spring·spring cloud·面试·mybatis·spring事务
Resky08181 小时前
什么是 Spring IOC:倒过来让容器帮你 new,而不是你到处 new
java·spring
AutumnWind04201 小时前
【JDK动态代理源码梳理】
java·后端·spring
AI进阶客栈1 小时前
开源 MQ Master:Spring Boot 统一管控 5 大消息队列
spring boot·后端·开源
暗夜猎手-大魔王1 小时前
转载--Hermes Agent 10 | 7 层安全防线:从用户授权到输入净化
java·数据库·安全
idolao3 小时前
Oligo 7.60 安装教程:引物设计+Java 环境配置
java·开发语言
做个文艺程序员6 小时前
第04篇:K8s 弹性伸缩实战:HPA、VPA、KEDA——Java SaaS 应对流量洪峰的秘密武器
java·容器·kubernetes·弹性伸缩·自动扩容·ai 推理伸缩
石山代码10 小时前
ArrayList / HashMap / ConcurrentHashMap
java·开发语言
AskHarries11 小时前
系统提示词、开发者指令和用户输入的优先级
java·前端·数据库