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")
publicStringtest(){
log.info("test---start");
try{
Thread.sleep(100000);
}catch(InterruptedExceptione){
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
我们发现请求已经到达服务,并且线程已经成功进入休眠,现在我们kill -15 14086结束进程
sudo kill -15 14086
1
回到日志
2020-04-2410:53:14.939INFO14086---[nio-9988-exec-1]com.ymy.controller.TestController:test---start
2020-04-2410:54:02.450INFO14086---[extShutdownHook]o.s.s.concurrent.ThreadPoolTaskExecutor:ShuttingdownExecutorService'applicationTaskExecutor'
java.lang.InterruptedException:sleepinterrupted
atjava.lang.Thread.sleep(NativeMethod)
atcom.ymy.controller.TestController.test(TestController.java:26)
atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
atjava.lang.reflect.Method.invoke(Method.java:498)
atorg.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
atorg.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
atorg.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
atorg.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
atorg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:634)
atorg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:741)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
atorg.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
atorg.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
atorg.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109)
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
atorg.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
atorg.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
atorg.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
atjava.lang.Thread.run(Thread.java:748)
2020-04-2410:54:04.574INFO14086---[nio-9988-exec-1]com.ymy.controller.TestController:test---end
2020-04-2410:54:04.610ERROR14086---[nio-9988-exec-1]o.s.web.servlet.HandlerExecutionChain:HandlerInterceptor.afterCompletionthrewexception
java.lang.NullPointerException:null
atorg.springframework.boot.actuate.metrics.web.servlet.LongTaskTimingHandlerInterceptor.stopLongTaskTimers(LongTaskTimingHandlerInterceptor.java:123)~[spring-boot-actuator-2.2.6.RELEASE.jar:2.2.6.RELEASE]
atorg.springframework.boot.actuate.metrics.web.servlet.LongTaskTimingHandlerInterceptor.afterCompletion(LongTaskTimingHandlerInterceptor.java:79)~[spring-boot-actuator-2.2.6.RELEASE.jar:2.2.6.RELEASE]
atorg.springframework.web.servlet.HandlerExecutionChain.triggerAfterCompletion(HandlerExecutionChain.java:179)~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.servlet.DispatcherServlet.triggerAfterCompletion(DispatcherServlet.java:1427)[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:634)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:741)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)[tomcat-embed-websocket-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109)[spring-boot-actuator-2.2.6.RELEASE.jar:2.2.6.RELEASE]
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)[tomcat-embed-core-9.0.33.jar:9.0.33]
atorg.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcat-embed-core-9.0.33.jar:9.0.33]
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[na:1.8.0_242]
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_242]
atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-9.0.33.jar:9.0.33]
atjava.lang.Thread.run(Thread.java:748)[na:1.8.0_242]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
居然报错了,但是test — end是打印出来了,为什么会报错呢?这就和sleep这个方法有关了,在线程休眠期间,当调用线程的interrupt方法的时候会导致sleep抛出异常
,这里很明显就是kill -15 这个命令会让程序马上调用线程的interrupt方法,目的是为了让线程停止,虽然让线程停止,但线程什么时候停止还是线程自己说的算,这就是为什么我们还能看到:test — end的原因。更多的 Spring Boot 视频教程可以关注微信订阅号码匠笔记回复 Spring Boot 获取。
ConfigurableApplicationContext colse
我们先看怎么实现
packagecom.ymy.controller;
importlombok.extern.slf4j.Slf4j;
importorg.springframework.beans.BeansException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.ApplicationContextAware;
importorg.springframework.context.ConfigurableApplicationContext;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.PostMapping;
importorg.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
publicclassTestControllerimplementsApplicationContextAware{
privateApplicationContextcontext;
@Override
publicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{
this.context=applicationContext;
}
@GetMapping(value="/test")
publicStringtest(){
log.info("test---start");
try{
Thread.sleep(100000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
log.info("test---end");
return"test";
}
/**
*停机
*/
@PostMapping(value="shutdown")
publicvoidshutdown(){
ConfigurableApplicationContextcyx=(ConfigurableApplicationContext)context;
cyx.close();
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344
重点在:cyx.close();
,为什么他能停止springboot项目呢?请看源码
publicvoidclose(){
synchronized(this.startupShutdownMonitor){
this.doClose();
if(this.shutdownHook!=null){
try{
Runtime.getRuntime().removeShutdownHook(this.shutdownHook);
}catch(IllegalStateExceptionvar4){
}
}
}
}
123456789101112
程序在启动的时候向jvm注册了一个关闭钩子,我们在执行colse方法的时候会删除这个关闭钩子,jvm就会知道这是需要停止服务。
我们看测试结果
很明显,他也出发了线程的interrupt方法导致线程报错,原理和kill -15差不多。
actuator
这种方式是通过引入依赖的方式停止服务,actuator提供了很多接口,比如健康检查,基本信息等等,我们也可以使用他来优雅的停机。
引入依赖
org.springframework.boot
spring-boot-starter-actuator
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)
publicStringtest(){
System.out.println("test---start");
try{
Thread.sleep(10000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
System.out.println("test---end");
return"hello";
}
1234567891011
在请求test途中停止服务
我们发现发送停止服务请求之后还给我们返回了提示信息,很人性化,我们看看控制台
test — end被执行了,不过在停止线程池的时候还是调用了线程的interrupt方法,导致sleep报错,这三种方式都可以比较优雅的停止springboot服务,如果我项目中存在线程休眠,我希望10秒以后再停止服务可以吗?肯定是可以的,我们只需要稍微做点修改就可以了。
1.新增停止springboot服务类:ElegantShutdownConfig.java
packagecom.ymy.config;
importorg.apache.catalina.connector.Connector;
importorg.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
importorg.springframework.context.ApplicationListener;
importorg.springframework.context.event.ContextClosedEvent;
importjava.util.concurrent.Executor;
importjava.util.concurrent.ThreadPoolExecutor;
importjava.util.concurrent.TimeUnit;
publicclassElegantShutdownConfigimplementsTomcatConnectorCustomizer,ApplicationListener<ContextClosedEvent>{
privatevolatileConnectorconnector;
privatefinalintwaitTime=10;
@Override
publicvoidcustomize(Connectorconnector){
this.connector=connector;
}
@Override
publicvoidonApplicationEvent(ContextClosedEventevent){
connector.pause();
Executorexecutor=connector.getProtocolHandler().getExecutor();
if(executorinstanceofThreadPoolExecutor){
try{
ThreadPoolExecutorthreadPoolExecutor=(ThreadPoolExecutor)executor;
threadPoolExecutor.shutdown();
if(!threadPoolExecutor.awaitTermination(waitTime,TimeUnit.SECONDS)){
System.out.println("请尝试暴力关闭");
}
}catch(InterruptedExceptionex){
System.out.println("异常了");
Thread.currentThread().interrupt();
}
}
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041
2.在启动类中加入bean
packagecom.ymy;
importcom.ymy.config.ElegantShutdownConfig;
importlombok.extern.slf4j.Slf4j;
importorg.apache.catalina.connector.Connector;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
importorg.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
importorg.springframework.boot.web.servlet.server.ServletWebServerFactory;
importorg.springframework.context.ApplicationListener;
importorg.springframework.context.ConfigurableApplicationContext;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.event.ContextClosedEvent;
importjava.util.concurrent.Executor;
importjava.util.concurrent.ThreadPoolExecutor;
importjava.util.concurrent.TimeUnit;
@SpringBootApplication
publicclassShutdownServerApplication{
publicstaticvoidmain(String[]args){
ConfigurableApplicationContextrun=SpringApplication.run(ShutdownServerApplication.class,args);
run.registerShutdownHook();
}
@Bean
publicElegantShutdownConfigelegantShutdownConfig(){
returnnewElegantShutdownConfig();
}
@Bean
publicServletWebServerFactoryservletContainer(){
TomcatServletWebServerFactorytomcat=newTomcatServletWebServerFactory();
tomcat.addConnectorCustomizers(elegantShutdownConfig());
returntomcat;
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041
这样我们就配置好了,我们再来测试一遍,test的接口还是休眠10秒
我们发现这次没有报错了,他是等待了一段时间之后再结束的线程池,这个时间就是我们在ElegantShutdownConfig类中配置的waitTime。
那可能你会有疑问了,jvm没有立即停止,那这个时候在有请求会发生什么呢?如果关闭的时候有新的请求,服务将不在接收此请求。
数据备份操作
如果我想在服务停止的时候做点备份操作啥的,应该怎么做呢?其实很简单在你要执行的方法上添加一个注解即可:@PreDestroy
“Destroy:消灭、毁灭 pre:前缀缩写
”
所以合在一起的意思就是在容器停止之前执行一次,你可以在这里面做备份操作,也可以做记录停机时间等。
新增服务停止备份工具类:DataBackupConfig.java
packagecom.ymy.config;
importorg.springframework.context.annotation.Configuration;
importjavax.annotation.PreDestroy;
@Configuration
publicclassDataBackupConfig{
@PreDestroy
publicvoidbackData(){
System.out.println("正在备份数据。。。。。。。。。。。");
}
}
123456789101112131415
我们再来测试然后打印控制台日志:
-
程序
+关注
关注
117文章
3785浏览量
81010 -
引擎
+关注
关注
1文章
361浏览量
22548 -
kill
+关注
关注
0文章
9浏览量
2104
原文标题:CTO 说了,如果发现谁用 kill -9 关闭程序就开除
文章出处:【微信号:magedu-Linux,微信公众号:马哥Linux运维】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论