Low-level API for Apache ZooKeeper client library providing connection management, retry policies, and basic ZooKeeper operations
npx @tessl/cli install tessl/maven-org-apache-curator--curator-client@4.3.0Apache Curator Client is the foundational low-level API component of Apache Curator, providing essential ZooKeeper client functionality with robust connection management, retry policies, and utility classes. It serves as the stable foundation for distributed applications requiring reliable ZooKeeper coordination.
pom.xml:<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.3.0</version>
</dependency>import org.apache.curator.CuratorZookeeperClient;
import org.apache.curator.RetryPolicy;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.ensemble.fixed.FixedEnsembleProvider;
import org.apache.curator.utils.ZKPaths;
import org.apache.curator.utils.PathUtils;
import org.apache.curator.utils.CloseableUtils;
import org.apache.curator.utils.ThreadUtils;import org.apache.curator.CuratorZookeeperClient;
import org.apache.curator.RetryPolicy;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.RetryLoop;
// Configure retry policy and create client
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorZookeeperClient client = new CuratorZookeeperClient(
"localhost:2181",
5000,
5000,
null,
retryPolicy
);
try {
// Start client connection
client.start();
client.blockUntilConnectedOrTimedOut();
// Use retry loop for ZooKeeper operations
RetryLoop retryLoop = client.newRetryLoop();
while (retryLoop.shouldContinue()) {
try {
// Perform ZooKeeper operations with client.getZooKeeper()
// ... your ZooKeeper operations here ...
retryLoop.markComplete();
} catch (Exception e) {
retryLoop.takeException(e);
}
}
} finally {
client.close();
}Apache Curator Client is organized around several key architectural components:
CuratorZookeeperClient provides robust connection handling with automatic reconnectionCore ZooKeeper client wrapper providing connection management, retry integration, and session handling for reliable distributed coordination.
public class CuratorZookeeperClient implements Closeable {
public CuratorZookeeperClient(String connectString, int sessionTimeoutMs,
int connectionTimeoutMs, Watcher watcher,
RetryPolicy retryPolicy);
public void start() throws Exception;
public void close() throws IOException;
public boolean isConnected();
public ZooKeeper getZooKeeper() throws Exception;
public RetryLoop newRetryLoop();
}Comprehensive retry policy implementations for handling transient failures and network issues in distributed ZooKeeper operations.
public interface RetryPolicy {
boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper);
}
public class ExponentialBackoffRetry implements RetryPolicy {
public ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries);
public ExponentialBackoffRetry(int baseSleepTimeMs, int maxRetries, int maxSleepMs);
}
public class RetryNTimes implements RetryPolicy {
public RetryNTimes(int n, int sleepMsBetweenRetries);
}Connection string providers for static and dynamic ZooKeeper cluster configuration, including integration with Netflix Exhibitor for service discovery.
public interface EnsembleProvider extends Closeable {
void start() throws Exception;
String getConnectionString();
void setConnectionString(String connectionString);
boolean updateServerListEnabled();
}
public class FixedEnsembleProvider implements EnsembleProvider {
public FixedEnsembleProvider(String connectionString);
}
public class ExhibitorEnsembleProvider implements EnsembleProvider {
// Constructor and methods for dynamic ensemble discovery
}Comprehensive path manipulation utilities for ZooKeeper node operations, including path validation, tree operations, and node management.
public class ZKPaths {
public static final String PATH_SEPARATOR = "/";
public static String makePath(String parent, String child);
public static String getNodeFromPath(String path);
public static PathAndNode getPathAndNode(String path);
public static List<String> split(String path);
public static void mkdirs(ZooKeeper zookeeper, String path) throws Exception;
public static void deleteChildren(ZooKeeper zookeeper, String path, boolean deleteSelf) throws Exception;
}Pluggable tracing framework for monitoring ZooKeeper operations, connection events, and performance metrics.
public interface TracerDriver {
void addTrace(String name, long time, TimeUnit unit);
void addCount(String name, int increment);
}
public class TimeTrace {
public TimeTrace(String name, TracerDriver driver);
public void commit();
}Advanced connection handling policies and thread-local retry management for fine-grained control over ZooKeeper connection behavior.
public interface ConnectionHandlingPolicy {
CheckTimeoutsResult checkTimeouts() throws Exception;
<T> T callWithRetry(CuratorZookeeperClient client, Callable<T> proc) throws Exception;
}
public class StandardConnectionHandlingPolicy implements ConnectionHandlingPolicy {
// Standard connection handling implementation
}public class CuratorConnectionLossException extends ConnectionLossException {
// Specialized ConnectionLossException for Curator-specific timeouts
}public enum ConnectionState {
CONNECTED,
SUSPENDED,
RECONNECTED,
LOST,
READ_ONLY
}public static class ZKPaths.PathAndNode {
public String getPath();
public String getNode();
}