欢迎光临
我们一直在努力

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

一、功能概述

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

二、使用背景

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

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

三、核心实现

3.1 类定义

@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 应用启动处理

@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 应用关闭处理

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 配置启用

在配置文件中设置:

# 是否启用启动日志
startLog.enabled=true

六、技术要点

6.1 ApplicationRunner 接口

public interface ApplicationRunner {
void run(ApplicationArguments args) throws Exception;
}

特点:

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

6.2 ShutdownHook

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// 清理逻辑
}));

特点:

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

6.3 优雅停机

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(run::close, 5, TimeUnit.SECONDS);

作用:

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

七、总结

ApplicationStartAndStopListener 实现了以下功能:

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

    • 系统运行时间统计
    • 系统稳定性监控
    • 故障排查和日志分析
    • 资源清理和优雅停机
    赞(0)
    未经允许不得转载:171主机测评 » 应用启动和关闭监听器功能分析
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址