简体   繁体   English

在线程错误时停止Spring Boot

[英]Stop Spring Boot on thread error

I have this configuration using Spring Boot: 我使用Spring Boot进行了以下配置:

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

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

When that thread stops (for any reason) I want to shut down the Spring Boot application. 当该线程停止(出于任何原因)时,我想关闭Spring Boot应用程序。

The thread is a long running process connecting to a database, webservice, socket... So it's quite likely that at some point it'll fail. 线程是连接到数据库,Web服务,套接字的长时间运行的进程,因此很有可能在某个时候失败。

What's the right way of shutting down Spring Boot when a thread stops? 线程停止时关闭Spring Boot的正确方法是什么? Should I not be using SimpleAsyncTaskExecutor from Spring? 我不应该使用Spring的SimpleAsyncTaskExecutor吗?

Thanks! 谢谢!

Spring Boot has a ShutdownEndpoint which Spring Boot具有一个ShutdownEndpoint

Allows the application to be gracefully shutdown (not enabled by default). 允许正常关闭应用程序(默认情况下未启用)。

You have to take a look on it: 您必须看一下:

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