Java Code Examples for org.apache.zookeeper.ZooKeeper#register()

The following examples show how to use org.apache.zookeeper.ZooKeeper#register() . 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: HandleHolder.java    From xian with Apache License 2.0 6 votes vote down vote up
private void internalClose() throws Exception
{
    try
    {
        ZooKeeper zooKeeper = (helper != null) ? helper.getZooKeeper() : null;
        if ( zooKeeper != null )
        {
            Watcher dummyWatcher = new Watcher()
            {
                @Override
                public void process(WatchedEvent event)
                {
                }
            };
            zooKeeper.register(dummyWatcher);   // clear the default watcher so that no new events get processed by mistake
            zooKeeper.close();
        }
    }
    catch ( InterruptedException dummy )
    {
        Thread.currentThread().interrupt();
    }
}
 
Example 2
Source File: GlobalZooKeeperCache.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public void start() throws IOException {
    CompletableFuture<ZooKeeper> zkFuture = zlClientFactory.create(globalZkConnect, SessionType.AllowReadOnly,
            zkSessionTimeoutMillis);

    // Initial session creation with global ZK must work
    try {
        ZooKeeper newSession = zkFuture.get(zkSessionTimeoutMillis, TimeUnit.MILLISECONDS);
        // Register self as a watcher to receive notification when session expires and trigger a new session to be
        // created
        newSession.register(this);
        zkSession.set(newSession);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.error("Failed to establish global zookeeper session: {}", e.getMessage(), e);
        throw new IOException(e);
    }
}
 
Example 3
Source File: ZKRMStateStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
@Private
@Unstable
protected synchronized ZooKeeper getNewZooKeeper()
    throws IOException, InterruptedException {
  ZooKeeper zk = new ZooKeeper(zkHostPort, zkSessionTimeout, null);
  zk.register(new ForwardingWatcher(zk));
  return zk;
}
 
Example 4
Source File: ZKRMStateStore.java    From big-c with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
@Private
@Unstable
protected synchronized ZooKeeper getNewZooKeeper()
    throws IOException, InterruptedException {
  ZooKeeper zk = new ZooKeeper(zkHostPort, zkSessionTimeout, null);
  zk.register(new ForwardingWatcher(zk));
  return zk;
}
 
Example 5
Source File: HandleHolder.java    From curator with Apache License 2.0 5 votes vote down vote up
private void internalClose(int waitForShutdownTimeoutMs) throws Exception
{
    try
    {
        ZooKeeper zooKeeper = (helper != null) ? helper.getZooKeeper() : null;
        if ( zooKeeper != null )
        {
            Watcher dummyWatcher = new Watcher()
            {
                @Override
                public void process(WatchedEvent event)
                {
                }
            };
            zooKeeper.register(dummyWatcher);   // clear the default watcher so that no new events get processed by mistake
            if ( waitForShutdownTimeoutMs == 0 )
            {
                zooKeeper.close();  // coming from closeAndReset() which is executed in ZK's event thread. Cannot use zooKeeper.close(n) otherwise we'd get a dead lock
            }
            else
            {
                zooKeeper.close(waitForShutdownTimeoutMs);
            }
        }
    }
    catch ( InterruptedException dummy )
    {
        Thread.currentThread().interrupt();
    }
}
 
Example 6
Source File: BlurServerShutDown.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public void register(final BlurShutdown shutdown, ZooKeeper zooKeeper) {
  this.shutdown = shutdown;
  this.zooKeeper = zooKeeper;
  zooKeeper.register(new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      KeeperState state = event.getState();
      if (state == KeeperState.Expired) {
        LOG.fatal("Zookeeper session has [" + state + "] server process shutting down.");
        shutdown.shutdown();
      }
    }
  });
}