【问题标题】:How to close spring boot embedded server如何关闭spring boot嵌入式服务器
【发布时间】:2021-05-18 22:49:21
【问题描述】:

我正在开发一个 cron,因为我正在使用 Web 客户端将发布请求发送到休息 api。我不想让嵌入式服务器保持打开状态,因为 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

【问题讨论】:

    标签: java spring rest


    【解决方案1】:

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

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

    original question link

    【讨论】:

    • 哦,它正在停止,但出现错误:Caused by: java.lang.IllegalStateException: org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@37c7595 has been closed already
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2019-10-11
    • 2015-09-30
    • 2011-03-02
    • 1970-01-01
    • 2017-08-14
    • 2018-07-12
    相关资源
    最近更新 更多