繁体   English   中英

在线程错误时停止Spring Boot

[英]Stop Spring Boot on thread error

我使用Spring Boot进行了以下配置:

@Bean
public TaskExecutor a() {
  return new SimpleAsyncTaskExecutor();
}

@Bean
public CommandLineRunner b() {
  return (String... args) -> {
    RunnableTask task = SomeRunnableTask();
    a().execute(task);
  };
}

当该线程停止(出于任何原因)时,我想关闭Spring Boot应用程序。

线程是连接到数据库,Web服务,套接字的长时间运行的进程,因此很有可能在某个时候失败。

线程停止时关闭Spring Boot的正确方法是什么? 我不应该使用Spring的SimpleAsyncTaskExecutor吗?

谢谢!

Spring Boot具有一个ShutdownEndpoint

允许正常关闭应用程序(默认情况下未启用)。

您必须看一下:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(500L); // For the response to be sent back to the requester
        }
        catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        ShutdownEndpoint.this.context.close();  // The ConfigurableApplicationContext 
    }
});
thread.setContextClassLoader(getClass().getClassLoader());
thread.start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM