private void actOnExists(final String path, final Runnable action) { // Watch for node existence. final AtomicBoolean nodeExists = new AtomicBoolean(false); Futures.addCallback( zkClient.exists( path, new Watcher() { @Override public void process(WatchedEvent event) { if (!shouldProcessZKEvent()) { return; } // When node is created, call the action. // Other event type would be handled by the action. if (event.getType() == Event.EventType.NodeCreated && nodeExists.compareAndSet(false, true)) { action.run(); } } }), new FutureCallback<Stat>() { @Override public void onSuccess(Stat result) { if (result != null && nodeExists.compareAndSet(false, true)) { action.run(); } } @Override public void onFailure(Throwable t) { LOG.error("Failed in exists call to {}. Shutting down service.", path, t); forceShutDown(); } }, Threads.SAME_THREAD_EXECUTOR); }