public class ZooKeeperConnection
extends java.lang.Object
implements org.springframework.context.SmartLifecycle
CuratorFramework instance whose lifecycle is managed as a Spring bean. Accepts
ZooKeeperConnectionListeners to be notified when connection or disconnection events are received.| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEFAULT_CLIENT_CONNECT_STRING
The default client connect string.
|
static int |
DEFAULT_CONNECTION_TIMEOUT
The default ZooKeeper connection timeout in milliseconds.
|
static int |
DEFAULT_INITIAL_RETRY_WAIT
The default initial number of milliseconds between connection retries.
|
static int |
DEFAULT_MAX_RETRY_ATTEMPTS
The default number of connection attempts that will be made after
a failed connection attempt.
|
static int |
DEFAULT_SESSION_TIMEOUT
The default ZooKeeper session timeout in milliseconds.
|
| Constructor and Description |
|---|
ZooKeeperConnection()
Establish a ZooKeeper connection with the default client connect string: "localhost:2181"
|
ZooKeeperConnection(java.lang.String clientConnectString)
Establish a ZooKeeper connection with the provided client connect string.
|
ZooKeeperConnection(java.lang.String clientConnectString,
java.lang.String namespace)
Establish a ZooKeeper connection with the provided client connect string and namespace.
|
ZooKeeperConnection(java.lang.String clientConnectString,
java.lang.String namespace,
int sessionTimeout,
int connectionTimeout,
int initialRetryWait,
int retryMaxAttempts)
Establish a ZooKeeper connection with the provided client connect string, namespace,
and timing values.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addListener(ZooKeeperConnectionListener listener)
Add a
ZooKeeperConnectionListener. |
org.apache.curator.framework.CuratorFramework |
getClient()
Provides access to the underlying
CuratorFramework instance. |
int |
getPhase() |
org.apache.curator.RetryPolicy |
getRetryPolicy()
Return the Curator retry policy.
|
boolean |
isAutoStartup() |
boolean |
isConnected()
Checks whether the underlying connection is established.
|
boolean |
isRunning()
Check whether this client is running.
|
boolean |
removeListener(ZooKeeperConnectionListener listener)
Remove a
ZooKeeperConnectionListener. |
void |
setAutoStartup(boolean autoStartup)
Set the flag that indicates whether this connection
should be started automatically.
|
void |
setRetryPolicy(org.apache.curator.RetryPolicy retryPolicy)
Set the Curator retry policy.
|
void |
start()
Starts the underlying
CuratorFramework instance. |
void |
stop()
Closes the underlying
CuratorFramework instance. |
void |
stop(java.lang.Runnable callback)
Closes the underlying
CuratorFramework instance and then invokes the callback. |
public static final java.lang.String DEFAULT_CLIENT_CONNECT_STRING
public static final int DEFAULT_SESSION_TIMEOUT
public static final int DEFAULT_CONNECTION_TIMEOUT
public static final int DEFAULT_INITIAL_RETRY_WAIT
public static final int DEFAULT_MAX_RETRY_ATTEMPTS
public ZooKeeperConnection()
public ZooKeeperConnection(java.lang.String clientConnectString)
clientConnectString - one or more host:port strings, comma-delimited if more than onepublic ZooKeeperConnection(java.lang.String clientConnectString,
java.lang.String namespace)
clientConnectString - one or more host:port strings, comma-delimited if more than onenamespace - the root path namespace in ZooKeeper (or default namespace if null)public ZooKeeperConnection(java.lang.String clientConnectString,
java.lang.String namespace,
int sessionTimeout,
int connectionTimeout,
int initialRetryWait,
int retryMaxAttempts)
This class uses ExponentialBackoffRetry internally.
clientConnectString - one or more host:port strings, comma-delimited if more than onenamespace - the root path namespace in ZooKeeper (or default namespace if null)sessionTimeout - ZooKeeper session timeout in millisecondsconnectionTimeout - ZooKeeper connection timeout in millisecondsinitialRetryWait - milliseconds between connection retries (base value)retryMaxAttempts - number of connection attempts that will be made after a failed connection attemptpublic boolean isConnected()
public org.apache.curator.framework.CuratorFramework getClient()
CuratorFramework instance.CuratorFramework instancepublic boolean addListener(ZooKeeperConnectionListener listener)
ZooKeeperConnectionListener.listener - the listener to addpublic boolean removeListener(ZooKeeperConnectionListener listener)
ZooKeeperConnectionListener.listener - the listener to removepublic boolean isAutoStartup()
isAutoStartup in interface org.springframework.context.SmartLifecyclepublic void setAutoStartup(boolean autoStartup)
autoStartup - if true, indicates this connection should
be started automaticallypublic void setRetryPolicy(org.apache.curator.RetryPolicy retryPolicy)
retryPolicy - Curator client RetryPolicypublic org.apache.curator.RetryPolicy getRetryPolicy()
public int getPhase()
getPhase in interface org.springframework.context.Phasedpublic boolean isRunning()
isRunning in interface org.springframework.context.Lifecyclepublic void start()
CuratorFramework instance.start in interface org.springframework.context.Lifecyclepublic void stop()
CuratorFramework instance.stop in interface org.springframework.context.Lifecyclepublic void stop(java.lang.Runnable callback)
CuratorFramework instance and then invokes the callback.stop in interface org.springframework.context.SmartLifecycle