如何使用关闭钩子优雅地停止Guava AbstractScheduledService?

时间:2012-08-30 22:06:53

标签: java multithreading service guava shutdown-hook

我正在使用带调度程序的AbstractScheduledService。一个简单的模式,如:

class MyService extends AbstractScheduledService {

   // KEEP THIS VAR IN MIND W.R.T the SHUTDOWN_HOOK BELOW
   public static volatile boolean keepRunning = true;

   protected void startUp() throws Exception {
     // startup stuff
   }

   protected void runOneIteration() throws Exception {
     // main logic stuff
   }

   protected void shutDown() throws Exception {
     // shutdown stuff
   }

   protected Scheduler scheduler() {
     return Scheduler.newFixedRateSchedule(0, 1, TimeUnit.SECONDS);
   }
 }

现在,我想实现一个典型的关闭钩子:(下面的代码片段将在main方法中)

final Thread mainThread = Thread.currentThread();
        LOGGER.debug("Adding the shutdown hook");
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                keepRunning = false;
                LOGGER.debug("The shutdown hook was engaged. Setting keepRunnning to false.");
                try {
                     // Is this appropriate?
                     mainThread.join();
                } catch (InterruptedException e) {
                    // handle exception here
                }
            }
        });

关闭钩子来自典型的docs示例。由于服务本身在不同的线程上运行,因此它似乎与Guava服务模式不兼容。

我的服务在runOneIteration()逻辑中有一个轮询循环。我希望它完成它当前的任务,然后在看到keepRunning现在为false时正常关闭,因为Shutdown钩子在最近一段时间忙于它当前的迭代时被占用了。

任何想法如何优雅地完成(完成当前迭代然后关闭)?

1 个答案:

答案 0 :(得分:6)

你不能只调用stopAndWait()吗?

Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            service.stopAsync().awaitTerminated(60, TimeUnit.SECONDS);
        }
});