High-level API that greatly simplifies using ZooKeeper.
—
Factory for creating and configuring CuratorFramework instances with comprehensive options for connection management, authentication, compression, namespacing, and error handling policies.
Main factory class for creating CuratorFramework instances with static factory methods and a comprehensive builder pattern.
/**
* Factory methods for creating framework-style clients
*/
public class CuratorFrameworkFactory {
/**
* Create a new client with default session timeout and default connection timeout
* @param connectString list of servers to connect to
* @param retryPolicy retry policy to use
* @return client
*/
public static CuratorFramework newClient(String connectString, RetryPolicy retryPolicy);
/**
* Create a new client
* @param connectString list of servers to connect to
* @param sessionTimeoutMs session timeout
* @param connectionTimeoutMs connection timeout
* @param retryPolicy retry policy to use
* @return client
*/
public static CuratorFramework newClient(
String connectString,
int sessionTimeoutMs,
int connectionTimeoutMs,
RetryPolicy retryPolicy
);
/**
* Create a new client with ZKClientConfig support (ZooKeeper 3.6.1+)
* @param connectString list of servers to connect to
* @param sessionTimeoutMs session timeout
* @param connectionTimeoutMs connection timeout
* @param retryPolicy retry policy to use
* @param zkClientConfig ZKClientConfig
* @return client
*/
public static CuratorFramework newClient(
String connectString,
int sessionTimeoutMs,
int connectionTimeoutMs,
RetryPolicy retryPolicy,
ZKClientConfig zkClientConfig
);
/**
* Return a new builder that builds a CuratorFramework
* @return new builder
*/
public static Builder builder();
/**
* Return the local address as bytes that can be used as a node payload
* @return local address bytes
*/
public static byte[] getLocalAddress();
}Usage Examples:
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
// Simple client creation
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(
"localhost:2181",
retryPolicy
);
// Client with custom timeouts
CuratorFramework client2 = CuratorFrameworkFactory.newClient(
"zk1:2181,zk2:2181,zk3:2181",
30000, // 30 second session timeout
10000, // 10 second connection timeout
new RetryNTimes(5, 2000)
);Comprehensive builder for configuring all aspects of CuratorFramework instances including connection parameters, authentication, compression, namespacing, and advanced policies.
public static class Builder {
/**
* Apply the current values and build a new CuratorFramework
* @return new CuratorFramework
*/
public CuratorFramework build();
/**
* Apply the current values and build a new temporary CuratorFramework
* @return temp instance
*/
public CuratorTempFramework buildTemp();
/**
* Apply the current values and build a new temporary CuratorFramework
* @param inactiveThreshold number of milliseconds of inactivity to cause connection close
* @param unit threshold unit
* @return temp instance
*/
public CuratorTempFramework buildTemp(long inactiveThreshold, TimeUnit unit);
/**
* Set the list of servers to connect to
* @param connectString list of servers to connect to
* @return this
*/
public Builder connectString(String connectString);
/**
* Set the list ensemble provider
* @param ensembleProvider the ensemble provider to use
* @return this
*/
public Builder ensembleProvider(EnsembleProvider ensembleProvider);
/**
* Configure if the ensemble configuration changes will be watched
* @param withEnsembleTracker use false if you want to avoid following ensemble configuration changes
* @return this
*/
public Builder ensembleTracker(boolean withEnsembleTracker);
/**
* @param sessionTimeoutMs session timeout
* @return this
*/
public Builder sessionTimeoutMs(int sessionTimeoutMs);
/**
* @param connectionTimeoutMs connection timeout
* @return this
*/
public Builder connectionTimeoutMs(int connectionTimeoutMs);
/**
* @param maxCloseWaitMs time to wait during close to join background threads
* @return this
*/
public Builder maxCloseWaitMs(int maxCloseWaitMs);
/**
* @param retryPolicy retry policy to use
* @return this
*/
public Builder retryPolicy(RetryPolicy retryPolicy);
/**
* @param threadFactory thread factory used to create Executor Services
* @return this
*/
public Builder threadFactory(ThreadFactory threadFactory);
/**
* Set namespace for all paths
* @param namespace the namespace
* @return this
*/
public Builder namespace(String namespace);
/**
* Add connection authorization
* @param scheme the scheme
* @param auth the auth bytes
* @return this
*/
public Builder authorization(String scheme, byte[] auth);
/**
* Add connection authorization
* @param authInfos list of AuthInfo objects with scheme and auth
* @return this
*/
public Builder authorization(List<AuthInfo> authInfos);
/**
* Set the data to use when PathAndBytesable.forPath(String) is used
* @param defaultData new default data to use
* @return this
*/
public Builder defaultData(byte[] defaultData);
/**
* @param compressionProvider the compression provider
* @return this
*/
public Builder compressionProvider(CompressionProvider compressionProvider);
/**
* Enable compression by default on all read and write calls
* @return this
*/
public Builder enableCompression();
/**
* @param zookeeperFactory the zookeeper factory to use
* @return this
*/
public Builder zookeeperFactory(ZookeeperFactory zookeeperFactory);
/**
* @param aclProvider a provider for ACLs
* @return this
*/
public Builder aclProvider(ACLProvider aclProvider);
/**
* @param canBeReadOnly if true, allow ZooKeeper client to enter read only mode
* @return this
*/
public Builder canBeReadOnly(boolean canBeReadOnly);
/**
* Turn off automatic container parent creation
* @return this
*/
public Builder dontUseContainerParents();
/**
* Set the error policy to use
* @param connectionStateErrorPolicy new error policy
* @return this
*/
public Builder connectionStateErrorPolicy(ConnectionStateErrorPolicy connectionStateErrorPolicy);
/**
* Set a timeout for CuratorZookeeperClient.close()
* @param waitForShutdownTimeoutMs default timeout
* @return this
*/
public Builder waitForShutdownTimeoutMs(int waitForShutdownTimeoutMs);
/**
* Set simulated session expiration percentage
* @param simulatedSessionExpirationPercent new simulated session expiration percentage
* @return this
*/
public Builder simulatedSessionExpirationPercent(int simulatedSessionExpirationPercent);
/**
* Set ZKClientConfig for ZooKeeper 3.6.1+
* @param zkClientConfig ZKClientConfig
* @return this
*/
public Builder zkClientConfig(ZKClientConfig zkClientConfig);
/**
* Add an enforced schema set
* @param schemaSet the schema set
* @return this
*/
public Builder schemaSet(SchemaSet schemaSet);
/**
* Set custom executor for safe operations
* @param runSafeService executor to use for calls to notifyAll from Watcher callbacks etc
* @return this
*/
public Builder runSafeService(Executor runSafeService);
/**
* Sets the connection state listener manager factory
* @param connectionStateListenerManagerFactory manager factory to use
* @return this
*/
public Builder connectionStateListenerManagerFactory(
ConnectionStateListenerManagerFactory connectionStateListenerManagerFactory
);
/**
* Set ZooKeeper compatibility mode
* @param zookeeperCompatibility compatibility mode
* @return this
*/
public Builder zookeeperCompatibility(ZookeeperCompatibility zookeeperCompatibility);
}Usage Examples:
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.framework.state.StandardConnectionStateErrorPolicy;
// Basic builder usage
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("localhost:2181")
.sessionTimeoutMs(30000)
.connectionTimeoutMs(10000)
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
.build();
// Advanced configuration
CuratorFramework advancedClient = CuratorFrameworkFactory.builder()
.connectString("zk1:2181,zk2:2181,zk3:2181")
.sessionTimeoutMs(60000)
.connectionTimeoutMs(15000)
.retryPolicy(new ExponentialBackoffRetry(1000, 5))
.namespace("myapp")
.authorization("digest", "user:password".getBytes())
.enableCompression()
.canBeReadOnly(true)
.connectionStateErrorPolicy(new StandardConnectionStateErrorPolicy())
.build();
// Temporary client for single operations over unreliable networks
CuratorTempFramework tempClient = CuratorFrameworkFactory.builder()
.connectString("remote-zk:2181")
.retryPolicy(new ExponentialBackoffRetry(2000, 10))
.buildTemp(TimeUnit.MINUTES.toMillis(5), TimeUnit.MILLISECONDS);Limited client interface designed for single requests to ZooKeeper ensembles over failure-prone networks such as WANs, with automatic connection cleanup after periods of inactivity.
/**
* Temporary CuratorFramework instances are meant for single requests to ZooKeeper
* ensembles over a failure prone network such as a WAN
*/
public interface CuratorTempFramework {
/**
* Stop the client
*/
void close();
/**
* Start a transaction builder
* @return builder object
*/
CuratorTransaction inTransaction();
/**
* Start a get data builder
* @return builder object
*/
TempGetDataBuilder getData();
}public class CuratorFrameworkFactory {
public static final int DEFAULT_SESSION_TIMEOUT_MS = 60000;
public static final int DEFAULT_CONNECTION_TIMEOUT_MS = 15000;
public static final long DEFAULT_INACTIVE_THRESHOLD_MS = 180000; // 3 minutes
public static final int DEFAULT_CLOSE_WAIT_MS = 1000;
public static final boolean DEFAULT_WITH_ENSEMBLE_TRACKER = true;
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-curator--curator-framework