![](/img/trans.png)
[英]Spring Boot - Web app is unable to stop MVStore writer thread
[英]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.