java - 如何触发关闭 Guava AbstractScheduledService?

标签 java service scheduled-tasks guava

我正在使用一些继承自 AbstractScheduledService 的服务,这些服务由 ServiceManager 管理。一切正常,但现在,有一个服务的 runOneIteration 需要相当长的时间,结果,我的进程终止时间太长(超过五秒)。

还有其他继承自AbstractExecutionThreadService的服务,也有类似的问题,我可以通过

@Override
protected final void triggerShutdown() {
    if (thread != null) thread.interrupt();
}

并在run 方法中存储private volatile thread。但是,如 this issue 中所述,AbstractScheduledService 没有 triggerShutdown .

我已经考虑过替代方案,例如让 runOneIteration 做更少的工作,但它既丑陋又低效。

我无法覆盖 stopAsync,因为它是最终的,我看不到任何其他内容。 做这样的事情有钩子(Hook)吗?

最佳答案

你能用这个吗?您有什么理由不能自己添加 triggerShutdown 吗?

class GuavaServer {
    public static void main(String[] args) throws InterruptedException {
        GuavaServer gs = new GuavaServer();
        Set<ForceStoppableScheduledService> services = new HashSet<>();
        ForceStoppableScheduledService ts = gs.new ForceStoppableScheduledService();
        services.add(ts);
        ServiceManager manager = new ServiceManager(services);
        manager.addListener(new Listener() {
            public void stopped() {
                System.out.println("Stopped");
            }

            public void healthy() {
                System.out.println("Health");
            }

            public void failure(Service service) {
                System.out.println("Failure");
                System.exit(1);
            }
        }, MoreExecutors.directExecutor());

        manager.startAsync(); // start all the services asynchronously
        Thread.sleep(3000);
        manager.stopAsync();
        //maybe make a manager.StopNOW()?
        for (ForceStoppableScheduledService service : services) {
            service.triggerShutdown();
        }
    }

    public class ForceStoppableScheduledService extends AbstractScheduledService {

        Thread thread;

        @Override
        protected void runOneIteration() throws Exception {
            thread = Thread.currentThread();
            try {
                System.out.println("Working");
                Thread.sleep(10000);
            } catch (InterruptedException e) {// can your long process throw InterruptedException?
                System.out.println("Thread was interrupted, Failed to complete operation");
            } finally {
                thread = null;
            }
            System.out.println("Done");
        }

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

        protected void triggerShutdown() {
            if (thread != null) thread.interrupt();
        }
    }
}

关于java - 如何触发关闭 Guava AbstractScheduledService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52581454/

相关文章:

java - Spring & 没有独特的 bean 类型

linux - 安装后在哪里可以找到 Linux 中的 Java SDK?

wcf - WCF 中的服务契约是什么?

angularjs - 如何在 angularJS 中为 $http.get 编写 karma-jasmine 测试用例?

java - 如何将值传递到树集中用户定义的比较函数中

java - 需要帮助将 curl 请求转换为 java 请求

android - 如何删除 Android Lollipop 中的前台通知?

windows - 附加数据 : Error Value: 2147942402. 如何修复?

multithreading - 先发制人如何运作?

git - 如何将代码从边缘节点部署到 hadoop 集群以使用 Oozie 对其进行调度?