2019-08-19 15:01:07 4702瀏覽
今天千鋒扣丁學(xué)堂Java培訓(xùn)老師給大家分享一篇關(guān)于Springboot停止服務(wù)方法的詳細(xì)介紹,首先在使用Springboot的時(shí)候,都要涉及到服務(wù)的停止和啟動(dòng),當(dāng)我們停止服務(wù)的時(shí)候,很多時(shí)候大家都是kill-9直接把程序進(jìn)程殺掉,這樣程序不會(huì)執(zhí)行優(yōu)雅的關(guān)閉。而且一些沒有執(zhí)行完的程序就會(huì)直接退出。
<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); }
第五種方法,自己寫一個(gè)Controller,然后將自己寫好的Controller獲取到程序的context,然后調(diào)用自己配置的Controller方法退出程序。通過調(diào)用自己寫的/shutDownContext方法關(guān)閉程序: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; } }
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入JavaEE/微服務(wù)VIP免費(fèi)公開課】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>