【问题标题】:Closing ApplicationContext still not shutting down springboot application关闭 ApplicationContext 仍然没有关闭 Spring Boot 应用程序
【发布时间】:2019-11-05 19:40:10
【问题描述】:

我有一个启动 Tomcat 的 spring-boot 应用程序。

我想在应用程序完成操作后关闭它。

我正在关注此blog 将其关闭。

它打印日志Spring Container is destroyed!,但我仍然需要按下 IntelliJ 中的停止按钮来停止它。

这里是@SpringBootApplication:

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    @Autowired
    MyService myService;

    @Autowired
    private ApplicationContext context;


    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        myService.getResult();
        System.out.println("-------> 1");
        ((ConfigurableApplicationContext)context).close();
    }
}

日志:

2019-11-06 00:51:57.018  INFO 26820 --- [           main] c.m.f.MyApplication  : Starting MyApplication on FTYUIN000171 with PID 26820 
2019-11-06 00:51:57.026  INFO 26820 --- [           main] c.m.f.MyApplication  : No active profile set, falling back to default profiles: default
2019-11-06 00:52:00.423  INFO 26820 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$74fff90c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-06 00:52:02.282  INFO 26820 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2019-11-06 00:52:02.358  INFO 26820 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-11-06 00:52:02.359  INFO 26820 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-11-06 00:52:02.796  INFO 26820 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-11-06 00:52:02.796  INFO 26820 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5304 ms
2019-11-06 00:52:08.093  INFO 26820 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-06 00:52:09.999  INFO 26820 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2019-11-06 00:52:10.010  INFO 26820 --- [           main] c.m.f.MyApplication  : Started MyApplication in 14.216 seconds (JVM running for 42.337)
-------> 1
2019-11-06 00:53:31.833  INFO 26820 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
Spring Container is destroyed!

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>            
    </dependency>               
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.4</version>
    </dependency>               
    <dependency>
        <groupId>com.fasterxml.uuid</groupId>
        <artifactId>java-uuid-generator</artifactId>
        <version>3.1.4</version>
    </dependency>   
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.9</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.1.0</version>
    </dependency>               
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.1.0</version>
    </dependency>   
</dependencies>

【问题讨论】:

    标签: java spring spring-boot tomcat


    【解决方案1】:

    默认情况下,Spring Boot 将 Web 应用程序部署到嵌入式 Tomcat。所以当你启动 Spring boot app 时,Tomcat 会启动,你的 web app 也会被部署。当您关闭 Spring 上下文时,只有您的 Web 应用停止,Tomcat 仍在运行(线程,如果有的话,也在运行)。

    所以你应该做的是使用System.exit(0);彻底杀死你的应用程序

    【讨论】:

    • 谢谢。但这听起来不像是优雅的关机。
    猜你喜欢
    • 2013-06-20
    • 1970-01-01
    • 2016-06-13
    • 2020-09-22
    • 2019-03-22
    • 2021-10-28
    • 2015-12-24
    • 2017-03-05
    • 2013-01-03
    相关资源
    最近更新 更多