@Test public void testSelfStoppingService() { int reacquireDelayMillis = 1500; ServiceTriggers triggers1 = new ServiceTriggers(); ServiceTriggers triggers2 = new ServiceTriggers(); LeaderService leader = newLeaderService(reacquireDelayMillis, TimeUnit.MILLISECONDS, supply( triggers1.listenTo(new AbstractScheduledService() { @Override protected void runOneIteration() throws Exception { stopAsync(); } @Override protected Scheduler scheduler() { return Scheduler.newFixedDelaySchedule(10, 10, TimeUnit.MILLISECONDS); } }), triggers2.listenTo(new NopService()))); leader.startAsync(); assertTrue(triggers1.getRunning().firedWithin(1, TimeUnit.MINUTES)); assertTrue(triggers1.getTerminated().firedWithin(1, TimeUnit.MINUTES)); assertTrue(triggers2.getRunning().firedWithin(1, TimeUnit.MINUTES)); }
private Service listenTo(Service delegate) { delegate.addListener(new Listener() { @Override public void starting() { } @Override public void running() { } @Override public void stopping(State from) { } @Override public void terminated(State from) { closeLeaderLatch(); } @Override public void failed(State from, Throwable failure) { closeLeaderLatch(); } }, MoreExecutors.directExecutor()); return delegate; }
LeaderService extends AbstractExecutionThreadService { private Service listenTo(Service delegate) { delegate.addListener(new Listener() { @Override public void starting() { } @Override public void running() { } @Override public void stopping(State from) { } @Override public void terminated(State from) { closeLeaderLatch(); } @Override public void failed(State from, Throwable failure) { closeLeaderLatch(); } }, MoreExecutors.directExecutor()); return delegate; } }
LeaderService extends AbstractExecutionThreadService { private Service listenTo(Service delegate) { delegate.addListener(new Listener() { @Override public void starting() { } @Override public void running() { } @Override public void stopping(State from) { } @Override public void terminated(State from) { closeLeaderLatch(); } @Override public void failed(State from, Throwable failure) { closeLeaderLatch(); } }, MoreExecutors.directExecutor()); return delegate; } LeaderService(CuratorFramework curator, String leaderPath, String instanceId, String serviceName,
                         long reacquireDelay, TimeUnit reacquireDelayUnit, Supplier<Service> serviceFactory); }
LeaderService extends AbstractExecutionThreadService { private Service listenTo(Service delegate) { delegate.addListener(new Listener() { @Override public void starting() { } @Override public void running() { } @Override public void stopping(State from) { } @Override public void terminated(State from) { closeLeaderLatch(); } @Override public void failed(State from, Throwable failure) { closeLeaderLatch(); } }, MoreExecutors.directExecutor()); return delegate; } LeaderService(CuratorFramework curator, String leaderPath, String instanceId, String serviceName,
                         long reacquireDelay, TimeUnit reacquireDelayUnit, Supplier<Service> serviceFactory); String getId(); Collection<Participant> getParticipants(); Participant getLeader(); boolean hasLeadership(); Optional<Service> getCurrentDelegateService(); }
LeaderService extends AbstractExecutionThreadService { private Service listenTo(Service delegate) { delegate.addListener(new Listener() { @Override public void starting() { } @Override public void running() { } @Override public void stopping(State from) { } @Override public void terminated(State from) { closeLeaderLatch(); } @Override public void failed(State from, Throwable failure) { closeLeaderLatch(); } }, MoreExecutors.directExecutor()); return delegate; } LeaderService(CuratorFramework curator, String leaderPath, String instanceId, String serviceName,
                         long reacquireDelay, TimeUnit reacquireDelayUnit, Supplier<Service> serviceFactory); String getId(); Collection<Participant> getParticipants(); Participant getLeader(); boolean hasLeadership(); Optional<Service> getCurrentDelegateService();  }