com.google.common.util.concurrent.AbstractIdleService Java Examples

The following examples show how to use com.google.common.util.concurrent.AbstractIdleService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: YarnTwillRunnerService.java    From twill with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an instance.
 *
 * @param config Configuration of the yarn cluster
 * @param zkConnect ZooKeeper connection string
 * @param locationFactory Factory to create {@link Location} instances that are readable and writable by this service
 */
public YarnTwillRunnerService(YarnConfiguration config, String zkConnect, LocationFactory locationFactory) {
  this.yarnConfig = config;
  this.locationFactory = locationFactory;
  this.zkClientService = getZKClientService(zkConnect);
  this.controllers = HashBasedTable.create();
  this.serviceDelegate = new AbstractIdleService() {
    @Override
    protected void startUp() throws Exception {
      YarnTwillRunnerService.this.startUp();
    }

    @Override
    protected void shutDown() throws Exception {
      YarnTwillRunnerService.this.shutDown();
    }
  };
}
 
Example #2
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
/** Test starting multiple instances that compete for leadership. */
@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 {
                        // Do nothing
                    }
                };
            }
        }).startAsync();
    }
    assertTrue(started.firedWithin(1, TimeUnit.MINUTES));
    // We know one service has started.  Wait a little while and verify no more services are started.
    Thread.sleep(250);
    assertTrue(startCount.get() == 1);
}