NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • 设计模式

  • 开发规范

  • 经验分享

  • 记录

  • 快速开始

    • Spring Boot整合RabbitMQ
    • Spring Boot整合Email邮件发送
    • Spring Boot整合jasypt加密配置文件
    • Spring Boot整合单元测试
    • Spring Boot整合优雅关机
      • 优雅关机
      • 优雅关闭线程池
      • 记一个坑
    • Spring Boot整合Redis分布式锁
    • Spring Boot整合MyBatis-plus
    • XXL-JOB快速上手
    • Spring Boot整合WebSocket(stomp协议)
    • SpringBoot整合i18n(多语言)
    • 第三方登录 - Google
    • 第三方登录 - Facebook
    • Spring Boot 整合Elasticsearch
  • 笔记

  • 面试题

  • 微服务

  • 踩过的坑

  • Java
  • 快速开始
NipGeihou
2022-02-26
目录

Spring Boot整合优雅关机

# 优雅关机

application.yml

server:
  shutdown: graceful # 开启优雅关机,默认IMMEDIATE是立即关机
  
management:
  endpoint:
    shutdown:
      enabled: true
  endpoints:
    web:
      exposure:
        include: "shutdown"
      base-path: /monitor

在使用若依框架实践中发现,似乎不需要此配置也可以实现优雅停机;而此并不会等待线程池的任务完成才关闭,需要再进行以下配置

# 优雅关闭线程池

    @Bean
    public ThreadPoolTaskExecutor collectThreadPoolExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(16);
        executor.setMaxPoolSize(96);
        executor.setQueueCapacity(1024);
        executor.setKeepAliveSeconds(30);
        executor.setThreadNamePrefix("xxxx任务");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        
        // 优雅关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        
        return executor;
    }

参考

  • Java 并发编程 - 线程池优雅关闭(四) - 掘金 (opens new window)
  • 线程池:ThreadPoolExecutor 和 ThreadPoolTaskExecutor 简述 – 古红色阿尔卑斯棒棒糖 (opens new window)

# 记一个坑

在实践中发现,使用在 Linux 下使用 kill -2 pid 并不是实现优雅关闭程序,需要配合 actuator

参考

  • How to gracefully handle the SIGKILL signal in Java - Stack Overflow (opens new window)
上次更新: 2022/12/31, 03:04:26
Spring Boot整合单元测试
Spring Boot整合Redis分布式锁

← Spring Boot整合单元测试 Spring Boot整合Redis分布式锁→

最近更新
01
元器件
05-23
02
iSCSI服务搭建
05-10
03
磁盘管理与文件系统
05-02
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式