Пример #1
0
  /**
   * Invoke this method to transition the service to the {@link State#FAILED}. The service will
   * <b>not be stopped</b> if it is running. Invoke this method when a service has failed critically
   * or otherwise cannot be started nor stopped.
   */
  protected final void notifyFailed(Throwable cause) {
    checkNotNull(cause);

    lock.lock();
    try {
      if (state == State.STARTING) {
        startup.setException(cause);
        shutdown.setException(new Exception("Service failed to start.", cause));
      } else if (state == State.STOPPING) {
        shutdown.setException(cause);
      } else if (state == State.RUNNING) {
        shutdown.setException(new Exception("Service failed while running", cause));
      } else if (state == State.NEW || state == State.TERMINATED) {
        throw new IllegalStateException("Failed while in state:" + state, cause);
      }
      failed(state, cause);
    } finally {
      lock.unlock();
    }
  }