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

The following examples show how to use com.google.common.util.concurrent.Service#stop() . 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: 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 2
Source File: Services.java    From twill with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the actual logic of chain Service start/stop.
 */
private static ListenableFuture<List<ListenableFuture<Service.State>>> doChain(boolean doStart,
                                                                               Service firstService,
                                                                               Service...moreServices) {
  SettableFuture<List<ListenableFuture<Service.State>>> resultFuture = SettableFuture.create();
  List<ListenableFuture<Service.State>> result = Lists.newArrayListWithCapacity(moreServices.length + 1);

  ListenableFuture<Service.State> future = doStart ? firstService.start() : firstService.stop();
  future.addListener(createChainListener(future, moreServices, new AtomicInteger(0), result, resultFuture, doStart),
                     Threads.SAME_THREAD_EXECUTOR);
  return resultFuture;
}
 
Example 3
Source File: ControllerTest.java    From twill with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerBefore() throws InterruptedException, ExecutionException, 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();

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

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

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

    try {
      controller.awaitTerminated(2, TimeUnit.SECONDS);
      Assert.fail("Service should not be terminated");
    } catch (TimeoutException e) {
      // Expected
    }

    service.stop();
    controller.awaitTerminated(120, TimeUnit.SECONDS);

  } finally {
    zkServer.stopAndWait();
  }
}
 
Example 4
Source File: OstrichOwnerGroup.java    From emodb with Apache License 2.0 5 votes vote down vote up
private void stopService(String name, Optional<? extends Service> ref) {
    if (ref.isPresent()) {
        Service service = ref.get();
        _log.info("Stopping owned service {}: {}", _group, name);
        service.stop();
    }
}