1、如果你的springboot应用打包成“可执行jar”,我们通过可以借助springboot launchscript优雅关闭。
2、如果你的springboot应用是普通jar,通过“java -jar”启动,那么优雅关闭的方式需要外部脚本去控制。
不过无论如何,我们希望做一件事情,就是在应用即将退出之前,关闭tomcat时,让tomcat不再接收新请求、但是已经接收的请求尽可能执行完毕。
为了达成这个设计目标,我们需要自定义tomcat链接器设置,主要思想是:当应用发出ContextClosedEvent时,关闭tomcat的链接处理器(即不再接收新请求),同时等待一段时间直到已有请求处理完毕,然后关闭内部的链接池、线程池。
import org.apache.catalina.connector.Connector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.coyote.ProtocolHandler; import org.apache.tomcat.util.threads.ThreadPoolExecutor; import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; /** * @author liuguanqing * created 2019/1/18 12:25 PM **/ public class MeteorTomcatConnectorCustomizer implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> { private volatile Connector connector; private static final int WAIT_TIMEOUT = 3000;//6S private static final int MAX_WAIT_TIMEOUT = 12000; protected static final Log logger = LogFactory.getLog(MeteorTomcatConnectorCustomizer.class); @Override public void customize(Connector connector) { this.connector = connector; } @Override public void onApplicationEvent(ContextClosedEvent event) { if (this.connector == null) { logger.warn("Tomcat connector has'nt been ready!"); } ProtocolHandler protocolHandler = this.connector.getProtocolHandler(); try { Executor executor = protocolHandler.getExecutor(); protocolHandler.stop();//执行关闭流程,此关闭流程中不会关闭外部指定的executer //非tomcat内置的executor,建议单独关闭 if (executor instanceof ThreadPoolExecutor) { try { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; int i = 0; while (i < MAX_WAIT_TIMEOUT && !threadPoolExecutor.isShutdown()) { threadPoolExecutor.shutdown(); if (!threadPoolExecutor.awaitTermination(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) { i += WAIT_TIMEOUT; logger.warn("Tomcat thread pool will be shutdown,total waiting-time=[" + i + "ms]"); } } if (!threadPoolExecutor.isShutdown()) { threadPoolExecutor.shutdownNow(); if (!threadPoolExecutor.awaitTermination(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) { logger.error("Tomcat thread pool did not terminate,shutdown failed!"); } } else { logger.info("Tomcat thread pool has been shutdown successfully!"); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } } catch (Exception e) { logger.error("Tomcat shutdown error!",e); } } }
相关推荐
prometheus监控nacos.orcal.springBoot.redis.mysql.tomcat.mq的指标和指标翻译
最新版本,18年8月份资源 为了支持reactive使用场景,内嵌的容器包...举个例子,如果你要使用TomcatEmbeddedServletContainerFactory回调接口来自定义内嵌Tomcat容器,你现在应该使用TomcatServletWebServerFactory。
* 后端: SpringBoot-2.x + Redis-4.x * 前端: Bootstrap + Jquery **测试环境** * IDEA + Maven-10.13 + Tomcat8 + JDK8 **启动说明** * 启动前,请配置好 [application.yml]...
后端: SpringBoot-2.x + Redis-4.x 前端: Bootstrap + Jquery 测试环境 IDEA + Maven-10.13 + Tomcat8 + JDK8 启动说明 启动前,请配置好 application.yml 中连接数据库的用户名和密码,以及Redis服务器的地址...
项目简介基于SpringBoot 2.x整合各种常用开发工具,包括但不限于Redis,MyBatisPlus,RocketMQ,RabbitMQ,Elasticsearch,Quartz,Xxl-Job,Kafka等。参考教程序号文章标题000102030405060708091011121314...
springboot2.x整合jsp,难点就是依赖、scope属性的使用
秒杀项目基于SSM 和 SpringBoot2.2.x 版重构 技术栈 后端: SSM版本:Spring + SpringMVC + MyBatis + Redis 点击: SpringBoot版本: SpringBoot2.2.x + Redis 点击: 前端:Bootstrap + Jquery 或者点击 branch ...
SpringBoot 是一个快速开发的框架,能够快速的整合第三方框架,简化XML配置,全部采用注解形式,内置Tomcat容器,帮助开发者能够实现快速开发,SpringBoot的Web组件 默认集成的是SpringMVC框架。 SpringMVC是控制层。
本系统基于springboot+mybatisplus+shiro+layui+redis整合开发的前后端分离权限管理系统,主要功能有: ...2.1.4数据库连接池:Alibaba Druid 1.0.2日志管理:SLF4J 1.7、Log4j前端框架:layui后台模板:layuimini
springboot简单示例可运行在tomcat6容器 ,本地在idea上已测试通过
初学SpringBoot遇到报错,反复检查后,反复查阅资料后,才知道是缺少依赖和tomcat版本不对
web项目是企业以及政府部门最常用的工具,政府门户网站,企业官网等基本都是web项目,web项目所使用的技术也是在不断的更新,前几年的技术基本上都是jsp+ssh,到...本文主要介绍了springboot+VUE怎么使用tomcat去部署
SpringBoot引用外部Tomcat.pdfSpringBoot引用外部Tomcat.pdfSpringBoot引用外部Tomcat.pdfSpringBoot引用外部Tomcat.pdfSpringBoot引用外部Tomcat.pdf
springboot框架 一、Spring Boot基础应用 Spring Boot特征 ...4.使部署变得简单,SpringBoot内置了三种Servlet容器,Tomcat,Jetty,undertow.我们只需要一个Java的运行环境就可以跑SpringBoot的项目了
特别注意:本商品为 Springboot 工作流 前后分离+...项目是maven 项目,全部iar通过maven管理百台运行基于springboot内置web容器,通过运行启动类运行,前端在tomcat、nginx运行数据库:咱这边用的 mysql5.6 jdk 需要 1.8
SpringBoot项目部署外部Tomcat容器简单示例,入门简单代码!
重点:SpringBoot-2.x + Redis-4.x 前置:Bootstrap + Jquery 测试环境 IDEA + Maven-10.13 + Tomcat8 + JDK8 启动说明 启动前,请配置好中连接数据库的用户名和密码,以及Redis服务器的地址和端口信息。 启动前,请...
SpringBoot 配置构建war部署到tomcat运行,直接package,生成war,放到tomcat的webapps目录下,启动可正常访问
springboot内置tomcat与外部tomcat配置https访问及http自动转向https
用springboot实现restful接口, 可运行在外部tomcat6不用手动执行Application, 在idea上运行ok