예제 #1
0
  @Override
  public void start() {
    log.info("Starting");
    if (!state.compareAndSet(CuratorFrameworkState.LATENT, CuratorFrameworkState.STARTED)) {
      IllegalStateException error = new IllegalStateException();
      log.error("Cannot be started more than once", error);
      throw error;
    }

    try {
      connectionStateManager.start(); // ordering dependency - must be called before client.start()
      client.start();
      executorService =
          Executors.newFixedThreadPool(2, threadFactory); // 1 for listeners, 1 for background ops

      executorService.submit(
          new Callable<Object>() {
            @Override
            public Object call() throws Exception {
              backgroundOperationsLoop();
              return null;
            }
          });
    } catch (Exception e) {
      handleBackgroundOperationException(null, e);
    }
  }
예제 #2
0
 private void validateConnection(CuratorEvent curatorEvent) {
   if (curatorEvent.getType() == CuratorEventType.WATCHED) {
     if (curatorEvent.getWatchedEvent().getState() == Watcher.Event.KeeperState.Disconnected) {
       connectionStateManager.addStateChange(ConnectionState.SUSPENDED);
       internalSync(
           this, "/",
           null); // we appear to have disconnected, force a new ZK event and see if we can connect
                  // to another server
     } else if (curatorEvent.getWatchedEvent().getState() == Watcher.Event.KeeperState.Expired) {
       connectionStateManager.addStateChange(ConnectionState.LOST);
     } else if (curatorEvent.getWatchedEvent().getState()
         == Watcher.Event.KeeperState.SyncConnected) {
       connectionStateManager.addStateChange(ConnectionState.RECONNECTED);
     } else if (curatorEvent.getWatchedEvent().getState()
         == Watcher.Event.KeeperState.ConnectedReadOnly) {
       connectionStateManager.addStateChange(ConnectionState.READ_ONLY);
     }
   }
 }
예제 #3
0
  @Override
  public void close() {
    log.debug("Closing");
    if (state.compareAndSet(CuratorFrameworkState.STARTED, CuratorFrameworkState.STOPPED)) {
      listeners.forEach(
          new Function<CuratorListener, Void>() {
            @Override
            public Void apply(CuratorListener listener) {
              CuratorEvent event =
                  new CuratorEventImpl(
                      CuratorFrameworkImpl.this,
                      CuratorEventType.CLOSING,
                      0,
                      null,
                      null,
                      null,
                      null,
                      null,
                      null,
                      null,
                      null);
              try {
                listener.eventReceived(CuratorFrameworkImpl.this, event);
              } catch (Exception e) {
                log.error("Exception while sending Closing event", e);
              }
              return null;
            }
          });

      listeners.clear();
      unhandledErrorListeners.clear();
      connectionStateManager.close();
      client.close();
      namespaceWatcherMap.close();
      executorService.shutdownNow();
    }
  }
예제 #4
0
  void logError(String reason, final Throwable e) {
    if ((reason == null) || (reason.length() == 0)) {
      reason = "n/a";
    }

    if (!Boolean.getBoolean(DebugUtils.PROPERTY_DONT_LOG_CONNECTION_ISSUES)
        || !(e instanceof KeeperException)) {
      log.error(reason, e);
    }

    if (e instanceof KeeperException.ConnectionLossException) {
      connectionStateManager.addStateChange(ConnectionState.LOST);
    }

    final String localReason = reason;
    unhandledErrorListeners.forEach(
        new Function<UnhandledErrorListener, Void>() {
          @Override
          public Void apply(UnhandledErrorListener listener) {
            listener.unhandledError(localReason, e);
            return null;
          }
        });
  }
예제 #5
0
 @Override
 public Listenable<ConnectionStateListener> getConnectionStateListenable() {
   return connectionStateManager.getListenable();
 }