@Test public void testMultipleLeaders() throws Exception { final Trigger started = new Trigger(); final AtomicInteger startCount = new AtomicInteger(); for (int i = 0; i < 5; i++) { newLeaderService(1, TimeUnit.HOURS, new Supplier<Service>() { @Override public Service get() { return new AbstractIdleService() { @Override protected void startUp() throws Exception { started.fire(); startCount.incrementAndGet(); } @Override protected void shutDown() throws Exception { } }; } }).startAsync(); } assertTrue(started.firedWithin(1, TimeUnit.MINUTES)); Thread.sleep(250); assertTrue(startCount.get() == 1); }
private synchronized void sleep(long waitNanos) throws InterruptedException { while (waitNanos > 0 && isRunning()) { long start = System.nanoTime(); TimeUnit.NANOSECONDS.timedWait(this, waitNanos); waitNanos -= System.nanoTime() - start; } }
LeaderService extends AbstractExecutionThreadService { private synchronized void sleep(long waitNanos) throws InterruptedException { while (waitNanos > 0 && isRunning()) { long start = System.nanoTime(); TimeUnit.NANOSECONDS.timedWait(this, waitNanos); waitNanos -= System.nanoTime() - start; } } }
LeaderService extends AbstractExecutionThreadService { private synchronized void sleep(long waitNanos) throws InterruptedException { while (waitNanos > 0 && isRunning()) { long start = System.nanoTime(); TimeUnit.NANOSECONDS.timedWait(this, waitNanos); waitNanos -= System.nanoTime() - start; } } LeaderService(CuratorFramework curator, String leaderPath, String instanceId, String serviceName,
                         long reacquireDelay, TimeUnit reacquireDelayUnit, Supplier<Service> serviceFactory); }
LeaderService extends AbstractExecutionThreadService { private synchronized void sleep(long waitNanos) throws InterruptedException { while (waitNanos > 0 && isRunning()) { long start = System.nanoTime(); TimeUnit.NANOSECONDS.timedWait(this, waitNanos); waitNanos -= System.nanoTime() - start; } } 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 synchronized void sleep(long waitNanos) throws InterruptedException { while (waitNanos > 0 && isRunning()) { long start = System.nanoTime(); TimeUnit.NANOSECONDS.timedWait(this, waitNanos); waitNanos -= System.nanoTime() - start; } } 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();  }