如何关闭spring-boot嵌入式服务器

41ik7eoe  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(329)

我正在开发一个cron,因为我正在使用一个web客户机向restapi发送post请求。我不想让嵌入式服务器继续运行,因为cron需要在几秒钟内完成它的任务。但当我不使用服务器时:

spring.main.web-application-type=none

它显示了错误 Connection prematurely closed BEFORE response; nested exception is reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response 因为它需要服务器工作,所以在响应之后我可以做些什么来停止服务器。
我试过了

server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=20s

但它什么也不做(我正在使用intellij ide)
更新:我试过了

SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);

但是得到 Caused by: java.lang.IllegalStateException: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@37c7595 has been closed already

qcuzuvrc

qcuzuvrc1#

这将确保springboot应用程序被正确关闭,资源被释放回操作系统,

@Autowired
private ApplicationContext context;

------------------
------------------
int exitCode = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);
System.exit(exitCode);

原始问题链接

相关问题