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

一、功能概述

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;
}

特点:

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

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

适用场景:

  • 系统运行时间统计
  • 系统稳定性监控
  • 故障排查和日志分析
  • 资源清理和优雅停机
相关推荐
Wang's Blog1 分钟前
Java 项目实战: 交友项目 - 登录发送验证码的测试验证与经验复盘
java·开发语言·交友
夕除11 分钟前
redis--主从还是集群,内存
java·spring·mybatis
百数平台14 分钟前
低代码安全技术实践:表单 / 流程 / 报表三级权限体系设计解析
java·大数据·数据库
白露与泡影17 分钟前
Spring Boot 实现验证码:Hutool-captcha、AJ-Captcha、EasyCaptcha 到底怎么选?
java·spring boot·后端
seconp24 分钟前
基于Spring Boot和Vue 3的智慧旅游网站:住宿预订、行程规划与千问接入
vue.js·spring boot·毕业设计·软件工程·课程设计·旅游·毕设
vx_bisheyuange26 分钟前
基于SpringBoot的校园数字化图书馆系统
spring boot·毕业设计
马优晨26 分钟前
Java中的dto是什么
java·开发语言·java中的dto是什么·java的dto是什么·dto在java中是什么
小蒜学长31 分钟前
基于Springboot+Vue的高校教师教学业绩综合管理系统的设计与开发(代码+数据库+LW)
java·数据库·spring boot·后端·教学业绩管理·三级权限
Mikko733 分钟前
Spring Boot 3.5.16 自带的 Tomcat 10.1.55 中了哪些 CVE?其中一条是修上一个漏洞时带进来的
java·spring boot·后端·安全·tomcat
十年Java程序媛37 分钟前
Java 内部类全解|成员 / 静态 / 匿名内部类,理清和 Lambda 的区别
java·spring boot·面试