簡體   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