maaartinus
maaartinus

Reputation: 46432

How to triggerShutdown a Guava AbstractScheduledService?

I'm using a few services inheriting from the AbstractScheduledService, which get managed by a ServiceManager. Everything works fine, but now, there's a service whose runOneIteration takes a rather long time, and as the result, my process takes too long to terminate (more than five seconds).

There are other services inheriting from AbstractExecutionThreadService, which had a similar problem, which I could solve via

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

and storing private volatile thread in the run method. However, there's no triggerShutdown for AbstractScheduledService as stated in this issue.

I already considered alternatives like making runOneIteration do less work, but it's both ugly and inefficient.

I can't override stopAsync as it's final and I can't see anything else. Is there a hook for doing something like this?

Upvotes: 4

Views: 911

Answers (1)

Charles
Charles

Reputation: 954

Can you work with this? Was there any reason you couldn't add a triggerShutdown yourself?

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();
        }
    }
}

Upvotes: 2

Related Questions