1.添加钩子函数,钩子函数中指定要调用的方法

@PostConstruct
public void run() {
this.zkClient.start(this);
this.schedulerService.start();
try {
logger.info("start Quartz server...");
QuartzExecutors.getInstance().start();
} catch (Exception e) {
try {
QuartzExecutors.getInstance().shutdown();
} catch (SchedulerException e1) {
logger.error("QuartzExecutors shutdown failed : " + e1.getMessage(), e1);
}
logger.error("start Quartz failed", e);
}
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (Stopper.isRunning()) {
close("shutdownHook");
}
}
}));
}

2.指定close方法要执行的一些动作

public void close(String cause) {

        try {
//execute only once
if (Stopper.isStopped()) {
return;
} logger.info("master server is stopping ..., cause : {}", cause); // set stop signal is true
Stopper.stop(); try {
//thread sleep 3 seconds for thread quietly stop
Thread.sleep(3000L);
} catch (Exception e) {
logger.warn("thread sleep exception ", e);
}
//close
this.schedulerService.close();
this.zkClient.close();
//close quartz
try {
QuartzExecutors.getInstance().shutdown();
logger.info("Quartz service stopped");
} catch (Exception e) {
logger.warn("Quartz service stopped exception:{}", e.getMessage());
}
} catch (Exception e) {
logger.error("master server stop exception ", e);
} finally {
// System.exit(-1);
}
}

3.使用kill命令验证

kill pid

优雅关闭springboot应用的更多相关文章

  1. 【积累】如何优雅关闭SpringBoot Web服务进程

    1.使用ps ef查出进程对应的pid. 2.使用kill -15 pid结束进程. 为什么不使用kill -9 pid,个人理解kill -15 pid更优雅,能在结束进程前执行spring容器清理 ...

  2. SpringBoot如何优雅关闭(SpringBoot2.3&Spring Boot2.2)

    SpringBoot如何优雅关闭(SpringBoot2.3&Spring Boot2.2) 优雅停止&暴力停止 暴力停止:像日常开发过程中,测试区或者本地开发时,我们并不会考虑项目关 ...

  3. 如何优雅关闭 Spring Boot 应用

    ## 前言 随着线上应用逐步采用 SpringBoot 构建,SpringBoot应用实例越来多,当线上某个应用需要升级部署时,常常简单粗暴地使用 kill 命令,这种停止应用的方式会让应用将所有处理 ...

  4. 如何关闭Springboot应用服务

    背景 以往的单机应用会采用kill方式关闭应用服务,但是这种关闭应用的方式在springboot中会让当前应用将所有处理中的请求丢弃,返回失败响应.我们在处理重要业务逻辑要极力避免的这种响应失败在,所 ...

  5. Spring-IOC 在非 web 环境下优雅关闭容器

    当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContex ...

  6. Socket编程中的强制关闭与优雅关闭及相关socket选项

    以下描述主要是针对windows平台下的TCP socket而言. 首先需要区分一下关闭socket和关闭TCP连接的区别,关闭TCP连接是指TCP协议层的东西,就是两个TCP端之间交换了一些协议包( ...

  7. 使用RunTime.getRunTime().addShutdownHook优雅关闭线程池

    有时候我们用到的程序不一定总是在JVM里面驻守,可能调用完就不用了,释放资源. RunTime.getRunTime().addShutdownHook的作用就是在JVM销毁前执行的一个线程.当然这个 ...

  8. 优雅关闭web服务的方式

    优雅关闭web服务 DBHelper, err = gorm.Open("mysql", "root:root@(115.159.59.129:3306)/test?ch ...

  9. 文件的读取与保存(try-with-resource优雅关闭)

    借鉴:https://www.cnblogs.com/itZhy/p/7636615.html 一.背景 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资 ...

随机推荐

  1. 1.8.7- HTML值label标签

    1.label直接进行包裹input就可以了.

  2. php与mysql 绑定变量和预定义处理

    非select 语句(没有结果集的) 1.建立连接数据库 $mysqli=new mysqli("localhost","root","", ...

  3. CString,string,char数组的转换

    来源:http://ticktick.blog.51cto.com/823160/317550 //----------------ANSI字符串转换为UNICODE字符串-------------- ...

  4. Hydra暴力破解工具的用法

    目录 Hydra 常见参数 破解SSH 破解FTP 破解HTTP 破解3389远程登录 Kali自带密码字典 dirb dirbuster fern-wifi metasploit wfuzz Hyd ...

  5. 【python】Leetcode每日一题-二叉搜索树节点最小距离

    [python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...

  6. Day003 彻底搞懂++、--

    彻底搞懂++.-- ++.--都是一目运算符 b=a++(把a的值先赋给b,a再自增1) b=++a(a先自增1,再赋给b) 通过一个例子理解 int a=1; int b=a++; int c=++ ...

  7. Docker配置阿里云镜像加速器及开启远程连接

    适用于CentOS-7版本 mkdir /etc/docker vim /etc/docker/deamon.json 添加以下内容配置镜像 { "registry-mirrors" ...

  8. 【小技巧】Eclipse 中创建Maven项目后没有WEB-INF文件夹以及web.xml文件

    懒得截图了,一张图配下面步骤搞定. 1.右键项目,选择propertities后选择图中①(被遮住了): 2.先不②勾选去掉,点击Apply:然后在把②处勾选上.此时④位置会出现东东,点击蓝色超链接. ...

  9. mysql 的查询操作语句---自动生成各种不同的序号

    1.通过查询语句添加自动生成序号 SELECT m.id,(@a :=@a + 1) AS a FROM 表名 m, (SELECT @a := 0) t1 2.MySQL字符串前后补0 前补0(LP ...

  10. Spring Cloud Gateway + Nacos(1)简单配置

    当初我学习时候就是参考这位大佬的博客: Nacos集成Spring Cloud Gateway 基础使用 现在学习到spring cloud alibaba 使用nacos做服务中心,dubbo做通信 ...