2019-08-19 15:01:07 3480浏览
今天千锋扣丁学堂Java培训老师给大家分享一篇关于Springboot停止服务方法的详细介绍,首先在使用Springboot的时候,都要涉及到服务的停止和启动,当我们停止服务的时候,很多时候大家都是kill-9直接把程序进程杀掉,这样程序不会执行优雅的关闭。而且一些没有执行完的程序就会直接退出。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
server.port=3333management.endpoint.shutdown.enabled=true management.endpoints.web.exposure.include=shutdown
package com.hqs.springboot.shutdowndemo.bean; import javax.annotation.PreDestroy; /** * @author huangqingshi * @Date 2019-08-17 */ public class TerminateBean { @PreDestroy public void preDestroy() { System.out.println("TerminalBean is destroyed"); } }
package com.hqs.springboot.shutdowndemo.config; import com.hqs.springboot.shutdowndemo.bean.TerminateBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author huangqingshi * @Date 2019-08-17 */ @Configuration public class ShutDownConfig { @Bean public TerminateBean getTerminateBean() { return new TerminateBean(); } }
curl -X POST http://localhost:3333/actuator/shutdown
/* method 2: use ctx.close to shutdown all application context */ ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } ctx.close();
/* method 3 : generate a pid in a specified path, while use command to shutdown pid : 'cat /Users/huangqingshi/app.pid | xargs kill' */ SpringApplication application = new SpringApplication(ShutdowndemoApplication.class); application.addListeners(new ApplicationPidFileWriter("/Users/huangqingshi/app.pid")); application.run();
/* method 4: exit this application using static method */ ConfigurableApplicationContext ctx = SpringApplication.run(ShutdowndemoApplication.class, args); exitApplication(ctx);
public static void exitApplication(ConfigurableApplicationContext context) { int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0); System.exit(exitCode); }
第五种方法,自己写一个Controller,然后将自己写好的Controller获取到程序的context,然后调用自己配置的Controller方法退出程序。通过调用自己写的/shutDownContext方法关闭程序:curl-XPOSThttp://localhost:3333/shutDownContext。
package com.hqs.springboot.shutdowndemo.controller; 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; /** * @author huangqingshi * @Date 2019-08-17 */ @RestController public class ShutDownController implements ApplicationContextAware { private ApplicationContext context; @PostMapping("/shutDownContext") public String shutDownContext() { ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context; ctx.close(); return "context is shutdown"; } @GetMapping("/") public String getIndex() { return "OK"; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } }
【关注微信公众号获取更多学习资料】 【扫码进入JavaEE/微服务VIP免费公开课】