Java Code Examples for com.google.common.util.concurrent.Service#State

The following examples show how to use com.google.common.util.concurrent.Service#State . 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: SimpleModuleInformationProvider.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
public ListenableFuture<Service.State> startModule(C5Module module) {
  SettableFuture<Service.State> startedFuture = SettableFuture.create();
  Service.Listener stateChangeListener = new SimpleC5ModuleListener(
      module,
      () -> {
        addRunningModule(module);
        startedFuture.set(null);
      },
      () -> removeModule(module),
      failureHandler);

  module.addListener(stateChangeListener, fiber);
  modules.put(module.getModuleType(), module);
  module.start();

  return startedFuture;
}
 
Example 2
Source File: Services.java    From twill with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link Runnable} that can be used as a {@link ListenableFuture} listener to trigger
 * further service action or completing the result future. Used by
 * {@link #doChain(boolean, com.google.common.util.concurrent.Service, com.google.common.util.concurrent.Service...)}
 */
private static Runnable createChainListener(final ListenableFuture<Service.State> future, final Service[] services,
                                            final AtomicInteger idx,
                                            final List<ListenableFuture<Service.State>> result,
                                            final SettableFuture<List<ListenableFuture<Service.State>>> resultFuture,
                                            final boolean doStart) {
  return new Runnable() {

    @Override
    public void run() {
      result.add(future);
      int nextIdx = idx.getAndIncrement();
      if (nextIdx == services.length) {
        resultFuture.set(result);
        return;
      }
      ListenableFuture<Service.State> actionFuture = doStart ? services[nextIdx].start() : services[nextIdx].stop();
      actionFuture.addListener(createChainListener(actionFuture, services, idx, result, resultFuture, doStart),
                               Threads.SAME_THREAD_EXECUTOR);
    }
  };
}
 
Example 3
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 4
Source File: ListenerExecutor.java    From twill with Apache License 2.0 6 votes vote down vote up
@Override
public void stopping(final Service.State from) {
  if (hasCalled(Service.State.STOPPING)) {
    return;
  }
  executor.execute(new Runnable() {
    @Override
    public void run() {
      try {
        delegate.stopping(from);
      } catch (Throwable t) {
        LOG.warn("Exception thrown from listener", t);
      }
    }
  });
}
 
Example 5
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 6
Source File: MainController.java    From cate with MIT License 5 votes vote down vote up
/**
 * Handle a network service failing.
 *
 * @param network the service which failed.
 * @param from the status the service was in before it failed.
 * @param thrwbl the exception causing the service to fail.
 */
public void onNetworkFailed(Network network, Service.State from, Throwable thrwbl) {
    networks.remove(network);
    Platform.runLater(() -> {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle(resources.getString("internalError.title"));
        alert.setContentText(thrwbl.getMessage());
        alert.showAndWait();
    });
}
 
Example 7
Source File: EventProcessorCell.java    From pravega with Apache License 2.0 5 votes vote down vote up
final void awaitStartupComplete() {
    try {
        // Wait for delegate to reach running state.
        delegate.awaitRunning();
    } catch (IllegalStateException e) {
        // If service state is NEW or RUNNING or STARTING, control wouldn't reach this point.
        Service.State state = delegate.state();
        // If service state is Stopping or Terminated, then startup has completed.
        if (state != Service.State.STOPPING && state != Service.State.TERMINATED) {
            // If service state is FAILED, throw error.
            throw e;
        }
    }
}
 
Example 8
Source File: Services.java    From pravega with Apache License 2.0 5 votes vote down vote up
@Override
public void terminated(@Nonnull Service.State from) {
    if (!this.invoked.compareAndSet(false, true)) {
        // Already invoked once. Don't double-call.
        return;
    }

    if (this.terminatedCallback != null) {
        this.terminatedCallback.run();
    }
}
 
Example 9
Source File: AbstractServiceListener.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public void failed(Service.State from, Throwable failure) {
}
 
Example 10
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void terminated(Service.State from) {
    addEvent(Service.State.TERMINATED);
}
 
Example 11
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void stopping(Service.State from) {
    _stopping.fire();
}
 
Example 12
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void failed(Service.State from, Throwable failure) {
    _failed.fire();
}
 
Example 13
Source File: ServiceFailureListener.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public void terminated(Service.State from) {
    // Do nothing
}
 
Example 14
Source File: ServiceFailureListener.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
public void stopping(Service.State from) {
    // Do nothing
}
 
Example 15
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void terminated(Service.State from) {
    _terminated.fire();
}
 
Example 16
Source File: ListenerExecutor.java    From twill with Apache License 2.0 4 votes vote down vote up
private boolean hasCalled(Service.State state) {
  return callStates.putIfAbsent(state, true) != null;
}
 
Example 17
Source File: LeaderServiceTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public void failed(Service.State from, Throwable failure) {
    _stoppedAt = System.currentTimeMillis();
}
 
Example 18
Source File: SimpleC5ModuleListener.java    From c5-replicator with Apache License 2.0 4 votes vote down vote up
@Override
public void terminated(Service.State from) {
  logger.info("Terminated module {}", module);
  onStoppingModule.run();
}
 
Example 19
Source File: ServiceListenerAdapter.java    From twill with Apache License 2.0 4 votes vote down vote up
@Override
public void failed(Service.State from, Throwable failure) {
  // No-op
}
 
Example 20
Source File: Services.java    From pravega with Apache License 2.0 2 votes vote down vote up
/**
 * Determines whether the given Service.State indicates the Service is either in the process of Stopping or already
 * Terminated or Failed.
 *
 * @param state The Service.State to test.
 * @return Whether this is a terminating state.
 */
public static boolean isTerminating(Service.State state) {
    return state == Service.State.STOPPING
            || state == Service.State.TERMINATED
            || state == Service.State.FAILED;
}