应用启动和关闭监听器功能分析
2026/6/26 16:07:27 网站建设 项目流程

一、功能概述

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

二、使用背景

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

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

三、核心实现

3.1 类定义

@Slf4j@ComponentpublicclassApplicationStartAndStopListenerimplementsApplicationRunner{privatefinalSysStartLogServicesysStartLogService;privatefinalConfigurableApplicationContextcontext;privatefinalbooleanstartLogEnabled;}

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

依赖注入:

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

3.2 应用启动处理

@Overridepublicvoidrun(ApplicationArgumentsargs){if(!startLogEnabled){return;}try{DatestartTime=newDate();log.info("启动时间:"+DateUtil.format(startTime,DatePattern.NORM_DATETIME_PATTERN));// 获取数据库中 id 最大的一条数据SysStartLogsysStartLog=sysStartLogService.getOne(Wrappers.<SysStartLog>lambdaQuery().orderByDesc(SysStartLog::getId).last("limit 1"));// 更新启动时间if(sysStartLog!=null){sysStartLog.setStartTime(startTime);sysStartLogService.updateById(sysStartLog);}}catch(Exceptione){log.warn("添加到系统启动日志表失败!"+e.getMessage());}shutdownHook(context);}

处理流程:

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

3.3 应用关闭处理

privatevoidshutdownHook(ConfigurableApplicationContextrun){Runtime.getRuntime().addShutdownHook(newThread(()->{DateendTime=newDate();System.out.println("开始执行优雅停机逻辑...");log.info("停机时间:"+DateUtil.format(endTime,DatePattern.NORM_DATETIME_PATTERN));// 添加到系统启动日志表SysStartLogsysStartLog=newSysStartLog();sysStartLog.setEndTime(endTime);sysStartLogService.saveOrUpdate(sysStartLog);// 延迟5秒关闭Spring Boot应用上下文ScheduledExecutorServiceexecutor=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 接口

publicinterfaceApplicationRunner{voidrun(ApplicationArgumentsargs)throwsException;}

特点:

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

6.2 ShutdownHook

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

特点:

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

6.3 优雅停机

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

作用:

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

七、总结

ApplicationStartAndStopListener实现了以下功能:

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

适用场景:

  • 系统运行时间统计
  • 系统稳定性监控
  • 故障排查和日志分析
  • 资源清理和优雅停机

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询