简体   繁体   English

记录 ExecutorService 关闭时正在处理的任务

[英]Logging tasks that were being processed by ExecutorService when it was shut down

I'm working on a Spring Boot application that accepts requests and processes them using an ExecutorService .我正在开发一个 Spring 引导应用程序,它接受请求并使用ExecutorService处理它们。 Each task submitted to the ExecutorService is non-iterative, long-running and is non-interruptible (in essence it submits the request in turn to several other services through a messaging broker, blocks for each of them, and waits for a reply).提交给ExecutorService的每个任务都是非迭代的、长时间运行的并且是不可中断的(本质上,它通过消息传递代理依次将请求提交给其他几个服务,为每个服务阻塞,并等待回复)。

I'm trying to implement graceful shutdown of the application in such a way that the application waits for a specified time for the ExecutorService to complete all running tasks and then shuts down.我正在尝试以这样一种方式实现应用程序的正常关闭,即应用程序等待指定时间让ExecutorService完成所有正在运行的任务然后关闭。 If, after the specified time has lapsed, there are unfinished tasks, I want the application to log the IDs of the tasks.如果在指定时间过去后有未完成的任务,我希望应用程序记录任务的 ID。

The relevant code is:相关代码为:

@Service
public class Service {

  private ExecutorService es = Executors.newFixedThreadPool(10);

  public handle(MyTask task) {
    es.execute(task);
  }

  @PreDestroy
  void destroy() {
    es.shutdown();
    try {
      boolean terminated = es.awaitTermination(60, TimeUnit.SECONDS);
      if (!terminated) {
      // I want to somehow log the IDs of MyTask's that haven't finished processing
      es.shutdownNow();
    } catch (InterruptedException e) {
      // Should not get here in the existing implementation as MyTask's are non- 
      // interruptible
    }
  }

How do I achieve this?我如何实现这一目标? I am contemplating two options but cannot wrap my head around how to implement either of them.我正在考虑两种选择,但无法解决如何实现其中任何一种。

  1. Wrap MyTask into another Runnable that would check in a loop for interruption and, if interrupted, log the id of MyTask .MyTask包装到另一个Runnable中,该 Runnable 将在循环中检查中断,如果被中断,则记录MyTask的 id。 So in essence make MyTask interruptible.所以本质上使MyTask中断。 How can this be implemented?如何实施?
  2. Save submitted MyTask s in a collection and on shutdown poll the collection for those that haven't completed and log their IDs.将提交的MyTask保存在一个集合中,并在关闭时轮询那些尚未完成的集合并记录他们的 ID。 The question here is how do I keep the collection current and remove those MyTask s that have been completed?这里的问题是如何使集合保持最新并删除那些已完成的MyTask

To reiterate, I'm implementing graceful shutdown of the whole application, not just the executor service.重申一下,我正在实现整个应用程序的正常关闭,而不仅仅是执行程序服务。 So the entire app shuts down after the specified period has lapsed.因此,整个应用程序会在指定时间段过后关闭。 Hence the need to log all tasks that haven't finished processing by the executor service.因此,需要记录执行器服务尚未完成处理的所有任务。

I think I might have found an option based on this post: implement an executor service that puts the task into a map and removes it once it's finished processing.我想我可能已经根据这篇文章找到了一个选项:实现一个执行器服务,将任务放入 map 并在完成处理后将其删除。 Upon shutdown, we iterate over the map and log the tasks that are still there.关机后,我们遍历 map 并记录仍然存在的任务。 Some of them might complete between logging and shutting the app down but that false positive is acceptable.其中一些可能在记录和关闭应用程序之间完成,但误报是可以接受的。

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

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