Java Code Examples for com.google.common.util.concurrent.Service#startAndWait()

The following examples show how to use com.google.common.util.concurrent.Service#startAndWait() . 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: CompositeService.java    From twill with Apache License 2.0 6 votes vote down vote up
@Override
protected void startUp() throws Exception {
  Throwable failureCause = null;

  for (Service service : services) {
    try {
      service.startAndWait();
    } catch (UncheckedExecutionException e) {
      failureCause = e.getCause();
      break;
    }
  }

  if (failureCause != null) {
    // Stop all running services and then throw the failure exception
    try {
      stopAll();
    } catch (Throwable t) {
      // Ignore the stop error. Just log.
      LOG.warn("Failed when stopping all services on start failure", t);
    }

    Throwables.propagateIfPossible(failureCause, Exception.class);
    throw new RuntimeException(failureCause);
  }
}
 
Example 2
Source File: ServicesTest.java    From twill with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompletion() throws ExecutionException, InterruptedException {
  Service service = new DummyService("s1", new AtomicBoolean());
  ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);

  service.start();
  service.stop();

  completion.get();

  AtomicBoolean transiting = new AtomicBoolean();
  service = new DummyService("s2", transiting);
  completion = Services.getCompletionFuture(service);

  service.startAndWait();
  transiting.set(true);
  service.stop();

  try {
    completion.get();
    Assert.assertTrue(false);
  } catch (ExecutionException e) {
    // Expected
  }
}
 
Example 3
Source File: ControllerTest.java    From twill with Apache License 2.0 5 votes vote down vote up
@Test
public void testController() throws ExecutionException, InterruptedException, TimeoutException {
  InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
  zkServer.startAndWait();

  LOG.info("ZKServer: " + zkServer.getConnectionStr());

  try {
    RunId runId = RunIds.generate();
    ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
    zkClientService.startAndWait();

    Service service = createService(zkClientService, runId);
    service.startAndWait();

    TwillController controller = getController(zkClientService, "testController", runId);
    controller.sendCommand(Command.Builder.of("test").build()).get(2, TimeUnit.SECONDS);
    controller.terminate().get(2, TimeUnit.SECONDS);

    final CountDownLatch terminateLatch = new CountDownLatch(1);
    service.addListener(new ServiceListenerAdapter() {
      @Override
      public void terminated(Service.State from) {
        terminateLatch.countDown();
      }
    }, Threads.SAME_THREAD_EXECUTOR);

    Assert.assertTrue(service.state() == Service.State.TERMINATED || terminateLatch.await(2, TimeUnit.SECONDS));

    zkClientService.stopAndWait();

  } finally {
    zkServer.stopAndWait();
  }
}
 
Example 4
Source File: ControllerTest.java    From twill with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerListener() throws InterruptedException {
  InMemoryZKServer zkServer = InMemoryZKServer.builder().build();
  zkServer.startAndWait();

  LOG.info("ZKServer: " + zkServer.getConnectionStr());
  try {
    RunId runId = RunIds.generate();
    ZKClientService zkClientService = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
    zkClientService.startAndWait();

    Service service = createService(zkClientService, runId);
    service.startAndWait();

    final CountDownLatch runLatch = new CountDownLatch(1);
    TwillController controller = getController(zkClientService, "testControllerListener", runId);
    controller.onRunning(new Runnable() {
      @Override
      public void run() {
        runLatch.countDown();
      }
    }, Threads.SAME_THREAD_EXECUTOR);

    Assert.assertTrue(runLatch.await(2, TimeUnit.SECONDS));

    service.stopAndWait();

    zkClientService.stopAndWait();
  } finally {
    zkServer.stopAndWait();
  }
}