共计 19704 个字符,预计需要花费 50 分钟才能阅读完成。
欢送微信搜寻公众号【java 版 web 我的项目】获取资源:java 学习视频 / 设计模式笔记 / 算法手册 /java 我的项目
kill -9 pid???
kill 可将指定的信息送至程序。预设的信息为 SIGTERM(15),可将指定程序终止。若仍无奈终止该程序,可应用 SIGKILL(9)信息尝试强制删除程序。程序或工作的编号可利用 ps 指令或 jobs 指令查看(这段话来自菜鸟教程)。
讲的这个简单,简略点来说就是用来杀死 linux 中的过程,啥?你问我啥是过程?请自行百度。
我置信很多人都用过kill -9 pid
这个命令,彻底杀死过程的意思,个别状况咱们应用它没有下面问题,然而在咱们我的项目中应用它就有可能存在致命的问题。
kill -9 pid 带来的问题
因为 kill -9 属于暴力删除,所以会给程序带来比较严重的结果,那到底会带来什么结果呢?
举个栗子:转账性能,再给两个账户进行加钱扣钱的时候忽然断电了?这个时候会产生什么事件?对于 InnoDB 存储引擎来说,没有什么损失,因为它反对事务,然而对于 MyISAM 引擎来说那几乎就是劫难,为什么?如果给 A 账户扣了钱,当初须要将 B 账户加钱,这个时候停电了,就会造成,A 的钱被扣了,然而 B 没有拿到这笔钱,这在生产环境是相对不容许的,kill -9 相当于忽然断电的成果。
当然了,像转账这种,必定不是应用 MyISAM 引擎,然而现在分布式火了起来,跨服务转账曾经是很平时的事件,这种时候如果应用 kill -9 去进行服务,那就不是你的事务能保证数据的准确性了,这个时候你可能会想到分布式事务,这个世界上没有相对的平安零碎或者架构,分布式事务也是一样,他也会存在问题,概率很小,如果一旦产生,损失有可能是无法弥补的,所以肯定不能应用 kill -9 去进行服务,因为你不晓得他会造成什么结果。
在 MyISAM 引擎中体现的更显著,比方用户的信息由两张表保护,管理员批改用户信息的时候须要批改两张表,但因为你的 kill -9 暴力完结我的项目,导致只批改胜利了一张表,这也会导致数据的不一致性,这是小事,因为大不了再批改一次,然而金钱、合同这些重要的信息如果因为你的暴力删除导致错乱,我感觉可能比删库跑路还重大,至多删库还能复原,你这个都不晓得错在哪里。
那咱们应该怎么完结我的项目呢?
其实 java 给咱们提供了完结我的项目的性能,比方:tomcat 能够应用 shutdown.bat/shutdown.sh 进行优雅完结。
什么叫优雅完结?
第一步:进行接管申请和外部线程。
第二步:判断是否有线程正在执行。
第三步:期待正在执行的线程执行结束。
第四步:进行容器。
以上四步才是失常的完结流程,那 springboot 怎么失常完结服务呢?上面我介绍几种失常完结服务的计划,请拿好小本本做好笔记。
优雅完结服务
kill -15 pid
这种形式也会比拟优雅的完结过程(我的项目),应用他的时候须要谨慎,为什么呢?咱们来看个例子
我写了一个一般的 controller 办法做测试
@GetMapping(value = "/test")
public String test(){log.info("test --- start");
try {Thread.sleep(100000);
} catch (InterruptedException e) {e.printStackTrace();
}
log.info("test --- end");
return "test";
}
1234567891011
代码很简略,打印:test — start 之后让让程序休眠 100 秒,而后再打印:test — end,在线程休眠中咱们应用 kill -15 pid 来完结这个过程,你们猜 test — end 会被打印吗?
application.yml
server:
port: 9988
12
启动我的项目
sudo mvn spring-boot:run
1
这是 maven 启动 springboot 我的项目的形式
看到这个就代表我的项目启动成了
找到我的项目的过程 id
sudo ps -ef |grep shutdown
1
这个就是我的项目的过程号,接下来咱们先测试 test 接口,让线程进入休眠状态,而后再应用 kill -15 14086 进行我的项目
sudo curl 127.0.0.1:9988/test
1
回到我的项目日志
咱们发现申请曾经达到服务,并且线程曾经胜利进入休眠,当初咱们 kill -15 14086 完结过程
sudo kill -15 14086
1
回到日志
2020-04-24 10:53:14.939 INFO 14086 --- [nio-9988-exec-1] com.ymy.controller.TestController : test --- start
2020-04-24 10:54:02.450 INFO 14086 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.ymy.controller.TestController.test(TestController.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
2020-04-24 10:54:04.574 INFO 14086 --- [nio-9988-exec-1] com.ymy.controller.TestController : test --- end
2020-04-24 10:54:04.610 ERROR 14086 --- [nio-9988-exec-1] o.s.web.servlet.HandlerExecutionChain : HandlerInterceptor.afterCompletion threw exception
java.lang.NullPointerException: null
at org.springframework.boot.actuate.metrics.web.servlet.LongTaskTimingHandlerInterceptor.stopLongTaskTimers(LongTaskTimingHandlerInterceptor.java:123) ~[spring-boot-actuator-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.actuate.metrics.web.servlet.LongTaskTimingHandlerInterceptor.afterCompletion(LongTaskTimingHandlerInterceptor.java:79) ~[spring-boot-actuator-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.web.servlet.HandlerExecutionChain.triggerAfterCompletion(HandlerExecutionChain.java:179) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletion(DispatcherServlet.java:1427) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109) [spring-boot-actuator-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_242]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_242]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_242]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
竟然报错了,然而 test — end 是打印进去了,为什么会报错呢?这就和 sleep 这个办法无关了,在线程休眠期间,当调用线程的 interrupt 办法的时候会导致 sleep 抛出异样
,这里很显著就是 kill -15 这个命令会让程序马上调用线程的 interrupt 办法,目标是为了让线程进行,尽管让线程进行,但线程什么时候进行还是线程本人说的算,这就是为什么咱们还能看到:test — end 的起因。
ConfigurableApplicationContext colse
咱们先看怎么实现
package com.ymy.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.context = applicationContext;}
@GetMapping(value = "/test")
public String test(){log.info("test --- start");
try {Thread.sleep(100000);
} catch (InterruptedException e) {e.printStackTrace();
}
log.info("test --- end");
return "test";
}
/**
* 停机
*/
@PostMapping(value = "shutdown")
public void shutdown(){ConfigurableApplicationContext cyx = (ConfigurableApplicationContext) context;
cyx.close();}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344
重点在:cyx.close();
,为什么他能进行 springboot 我的项目呢?请看源码
public void close() {synchronized(this.startupShutdownMonitor) {this.doClose();
if (this.shutdownHook != null) {
try {Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
} catch (IllegalStateException var4) {}}
}
}
123456789101112
程序在启动的时候向 jvm 注册了一个敞开钩子,咱们在执行 colse 办法的时候会删除这个敞开钩子,jvm 就会晓得这是须要进行服务。
咱们看测试后果
很显著,他也登程了线程的 interrupt 办法导致线程报错,原理和 kill -15 差不多。
actuator
这种形式是通过引入依赖的形式进行服务,actuator 提供了很多接口,比方健康检查,根本信息等等,咱们也能够应用他来优雅的停机。
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
12345
application.yml
server:
port: 9988
management:
endpoints:
web:
exposure:
include: shutdown
endpoint:
shutdown:
enabled: true
server:
port: 8888
12345678910111213
我这里对 actuator 的接口从新给定了一个接口,这样可进步安全性,上面咱们来测试一下
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){System.out.println("test --- start");
try {Thread.sleep(10000);
} catch (InterruptedException e) {e.printStackTrace();
}
System.out.println("test --- end");
return "hello";
}
1234567891011
在申请 test 途中进行服务
咱们发现发送进行服务申请之后还给咱们返回了提示信息,很人性化,咱们看看控制台
test — end 被执行了,不过在进行线程池的时候还是调用了线程的 interrupt 办法,导致 sleep 报错,这三种形式都能够比拟优雅的进行 springboot 服务,如果我我的项目中存在线程休眠,我心愿 10 秒当前再进行服务能够吗?必定是能够的,咱们只须要略微做点批改就能够了。
1. 新增进行 springboot 服务类:ElegantShutdownConfig.java
package com.ymy.config;
import org.apache.catalina.connector.Connector;
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.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ElegantShutdownConfig implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
private volatile Connector connector;
private final int waitTime = 10;
@Override
public void customize(Connector connector) {this.connector = connector;}
@Override
public void onApplicationEvent(ContextClosedEvent event) {connector.pause();
Executor executor = connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {System.out.println("请尝试暴力敞开");
}
} catch (InterruptedException ex) {System.out.println("异样了");
Thread.currentThread().interrupt();
}
}
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041
2. 在启动类中退出 bean
package com.ymy;
import com.ymy.config.ElegantShutdownConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextClosedEvent;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@SpringBootApplication
public class ShutdownServerApplication {public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(ShutdownServerApplication.class, args);
run.registerShutdownHook();}
@Bean
public ElegantShutdownConfig elegantShutdownConfig() {return new ElegantShutdownConfig();
}
@Bean
public ServletWebServerFactory servletContainer() {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers(elegantShutdownConfig());
return tomcat;
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041
这样咱们就配置好了,咱们再来测试一遍,test 的接口还是休眠 10 秒
咱们发现这次没有报错了,他是期待了一段时间之后再完结的线程池,这个工夫就是咱们在 ElegantShutdownConfig 类中配置的 waitTime。
那可能你会有疑难了,jvm 没有立刻进行,那这个时候在有申请会产生什么呢?如果敞开的时候有新的申请,服务将不在接管此申请。
数据备份操作
如果我想在服务进行的时候做点备份操作啥的,应该怎么做呢?其实很简略在你要执行的办法上增加一个注解即可:@PreDestroy
Destroy:毁灭、覆灭
pre: 前缀缩写
所以合在一起的意思就是在容器进行之前执行一次,你能够在这外面做备份操作,也能够做记录停机工夫等。
新增服务进行备份工具类:DataBackupConfig.java
package com.ymy.config;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PreDestroy;
@Configuration
public class DataBackupConfig {
@PreDestroy
public void backData(){System.out.println("正在备份数据。。。。。。。。。。。");
}
}
123456789101112131415
咱们再来测试而后打印控制台日志:
作者:流星 007
起源:https://blog.csdn.net/qq_3322…
题目:还在应用 kill -9 pid 完结 spring boot 我的项目吗?无妨试试这几种优雅的形式!
近期热文举荐:
SpringCloud 微服务电商我的项目教程