예제 #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;
  }
  @Override
  public void process(WatchedEvent event) {
    if (LOG_EVENTS) {
      log.debug("ConnectState watcher: " + event);
    }

    for (Watcher parentWatcher : parentWatchers) {
      TimeTrace timeTrace = new TimeTrace("connection-state-parent-process", tracer.get());
      parentWatcher.process(event);
      timeTrace.commit();
    }

    boolean wasConnected = isConnected.get();
    boolean newIsConnected = wasConnected;
    if (event.getType() == Watcher.Event.EventType.None) {
      newIsConnected = checkState(event.getState(), wasConnected);
    }

    if (newIsConnected != wasConnected) {
      isConnected.set(newIsConnected);
      connectionStartMs = System.currentTimeMillis();
    }
  }