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

The following examples show how to use com.google.common.util.concurrent.Service#addListener() . 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: Services.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Attaches the given callbacks which will be invoked when the given Service enters a TERMINATED or FAILED state.
 * The callbacks are optional and may be invoked synchronously if the Service is already in one of these states.
 *
 * @param service            The Service to attach to.
 * @param terminatedCallback (Optional) A Runnable that will be invoked if the Service enters a TERMINATED state.
 * @param failureCallback    (Optional) A Runnable that will be invoked if the Service enters a FAILED state.
 * @param executor           An Executor to use for callback invocations.
 */
public static void onStop(Service service, Runnable terminatedCallback, Consumer<Throwable> failureCallback, Executor executor) {
    ShutdownListener listener = new ShutdownListener(terminatedCallback, failureCallback);
    service.addListener(listener, executor);

    // addListener() will not invoke the callbacks if the service is already in a terminal state. As such, we need to
    // manually check for these states after registering the listener and invoke the appropriate callback. The
    // ShutdownListener will make sure they are not invoked multiple times.
    Service.State state = service.state();
    if (state == Service.State.FAILED) {
        // We don't care (or know) the state from which we came, so we just pass some random one.
        listener.failed(Service.State.FAILED, service.failureCause());
    } else if (state == Service.State.TERMINATED) {
        listener.terminated(Service.State.TERMINATED);
    }
}
 
Example 2
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 3
Source File: Services.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Asynchronously starts a Service and returns a CompletableFuture that will indicate when it is running.
 *
 * @param service  The Service to start.
 * @param executor An Executor to use for callback invocations.
 * @return A CompletableFuture that will be completed when the service enters a RUNNING state, or completed
 * exceptionally if the service failed to start.
 */
public static CompletableFuture<Void> startAsync(Service service, Executor executor) {
    // Service.startAsync() will fail if the service is not in a NEW state. That is, if it is already RUNNING or
    // STARTED, then the method will fail synchronously, hence we are not in danger of not invoking our callbacks,
    // as long as we register the Listener before we attempt to start.
    // Nevertheless, do make a sanity check since once added, a Listener cannot be removed.
    Preconditions.checkState(service.state() == Service.State.NEW,
            "Service expected to be %s but was %s.", Service.State.NEW, service.state());
    Preconditions.checkNotNull(executor, "executor");
    CompletableFuture<Void> result = new CompletableFuture<>();
    service.addListener(new StartupListener(result), executor);
    service.startAsync();
    return result;
}
 
Example 4
Source File: ServiceFailureListener.java    From emodb with Apache License 2.0 4 votes vote down vote up
public static void listenTo(Service service, MetricRegistry metricRegistry) {
    service.addListener(new ServiceFailureListener(service, metricRegistry), MoreExecutors.sameThreadExecutor());
}
 
Example 5
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
private static Service trackEvents(String id, List<Event> events, Service service) {
    service.addListener(new EventListener(id, events), MoreExecutors.directExecutor());
    return service;
}