Ejemplo n.º 1
0
  /**
   * This method blocks until the connection to ZK succeeds. Use with caution. The block will
   * timeout after the connection timeout (as passed to the constructor) has elapsed
   *
   * @return true if the connection succeeded, false if not
   * @throws InterruptedException interrupted while waiting
   */
  public boolean blockUntilConnectedOrTimedOut() throws InterruptedException {
    Preconditions.checkArgument(started.get());

    log.debug("blockUntilConnectedOrTimedOut() start");
    TimeTrace trace = startTracer("blockUntilConnectedOrTimedOut");

    internalBlockUntilConnectedOrTimedOut();

    trace.commit();

    boolean localIsConnected = state.isConnected();
    log.debug("blockUntilConnectedOrTimedOut() end. isConnected: " + localIsConnected);

    return localIsConnected;
  }
Ejemplo n.º 2
0
  /**
   * Convenience utility: creates a retry loop calling the given proc and retrying if needed
   *
   * @param client Zookeeper
   * @param proc procedure to call with retry
   * @param <T> return type
   * @return procedure result
   * @throws Exception any non-retriable errors
   */
  public static <T> T callWithRetry(CuratorZookeeperClient client, Callable<T> proc)
      throws Exception {
    T result = null;
    RetryLoop retryLoop = client.newRetryLoop();
    while (retryLoop.shouldContinue()) {
      try {
        client.internalBlockUntilConnectedOrTimedOut();

        result = proc.call();
        retryLoop.markComplete();
      } catch (Exception e) {
        retryLoop.takeException(e);
      }
    }
    return result;
  }