Package org.apache.curator
Class CuratorZookeeperClient
- java.lang.Object
-
- org.apache.curator.CuratorZookeeperClient
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class CuratorZookeeperClient extends java.lang.Object implements java.io.CloseableA wrapper around Zookeeper that takes care of some low-level housekeeping
-
-
Constructor Summary
Constructors Constructor Description CuratorZookeeperClient(java.lang.String connectString, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy)CuratorZookeeperClient(EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy)CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, int waitForShutdownTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly)CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanblockUntilConnectedOrTimedOut()This method blocks until the connection to ZK succeeds.voidclose()Close the client.voidclose(int waitForShutdownTimeoutMs)Close this client object as theclose()method.ConnectionHandlingPolicygetConnectionHandlingPolicy()Return the configured connection handling policyintgetConnectionTimeoutMs()Return the configured connection timeoutjava.lang.StringgetCurrentConnectionString()Returns the current known connection string - not guaranteed to be correct value at any point in the future.longgetInstanceIndex()Every time a newZooKeeperinstance is allocated, the "instance index" is incremented.intgetLastNegotiatedSessionTimeoutMs()Return the most recent value ofZooKeeper.getSessionTimeout()or 0RetryPolicygetRetryPolicy()Return the current retry policyTracerDrivergetTracerDriver()Return the current tracing driverorg.apache.zookeeper.ZooKeepergetZooKeeper()Return the managed ZK instance.voidinternalBlockUntilConnectedOrTimedOut()For internal use onlybooleanisConnected()Returns true if the client is current connectedRetryLoopnewRetryLoop()Return a new retry loop.SessionFailRetryLoopnewSessionFailRetryLoop(SessionFailRetryLoop.Mode mode)Return a new "session fail" retry loop.voidreset()For internal use only - reset the internally managed ZK handlevoidsetRetryPolicy(RetryPolicy policy)Change the retry policyvoidsetTracerDriver(TracerDriver tracer)Change the tracing drivervoidstart()Must be called after constructionOperationTracestartAdvancedTracer(java.lang.String name)Start a new advanced tracer with more metrics being recordedTimeTracestartTracer(java.lang.String name)Start a new tracer
-
-
-
Constructor Detail
-
CuratorZookeeperClient
public CuratorZookeeperClient(java.lang.String connectString, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy)- Parameters:
connectString- list of servers to connect tosessionTimeoutMs- session timeoutconnectionTimeoutMs- connection timeoutwatcher- default watcher or nullretryPolicy- the retry policy to use
-
CuratorZookeeperClient
public CuratorZookeeperClient(EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy)
- Parameters:
ensembleProvider- the ensemble providersessionTimeoutMs- session timeoutconnectionTimeoutMs- connection timeoutwatcher- default watcher or nullretryPolicy- the retry policy to use
-
CuratorZookeeperClient
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly)
- Parameters:
zookeeperFactory- factory for creatingZooKeeperinstancesensembleProvider- the ensemble providersessionTimeoutMs- session timeoutconnectionTimeoutMs- connection timeoutwatcher- default watcher or nullretryPolicy- the retry policy to usecanBeReadOnly- if true, allow ZooKeeper client to enter read only mode in case of a network partition. SeeZooKeeper(String, int, Watcher, long, byte[], boolean)for details
-
CuratorZookeeperClient
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
- Parameters:
zookeeperFactory- factory for creatingZooKeeperinstancesensembleProvider- the ensemble providersessionTimeoutMs- session timeoutconnectionTimeoutMs- connection timeoutwatcher- default watcher or nullretryPolicy- the retry policy to usecanBeReadOnly- if true, allow ZooKeeper client to enter read only mode in case of a network partition. SeeZooKeeper(String, int, Watcher, long, byte[], boolean)for detailsconnectionHandlingPolicy- connection handling policy - use one of the pre-defined policies or write your own- Since:
- 3.0.0
-
CuratorZookeeperClient
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, int waitForShutdownTimeoutMs, org.apache.zookeeper.Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly, ConnectionHandlingPolicy connectionHandlingPolicy)
- Parameters:
zookeeperFactory- factory for creatingZooKeeperinstancesensembleProvider- the ensemble providersessionTimeoutMs- session timeoutconnectionTimeoutMs- connection timeoutwaitForShutdownTimeoutMs- default timeout fo close operationwatcher- default watcher or nullretryPolicy- the retry policy to usecanBeReadOnly- if true, allow ZooKeeper client to enter read only mode in case of a network partition. SeeZooKeeper(String, int, Watcher, long, byte[], boolean)for detailsconnectionHandlingPolicy- connection handling policy - use one of the pre-defined policies or write your own- Since:
- 4.0.2
-
-
Method Detail
-
getZooKeeper
public org.apache.zookeeper.ZooKeeper getZooKeeper() throws java.lang.ExceptionReturn the managed ZK instance.- Returns:
- client the client
- Throws:
java.lang.Exception- if the connection timeout has elapsed or an exception occurs in a background process
-
newRetryLoop
public RetryLoop newRetryLoop()
Return a new retry loop. All operations should be performed in a retry loop- Returns:
- new retry loop
-
newSessionFailRetryLoop
public SessionFailRetryLoop newSessionFailRetryLoop(SessionFailRetryLoop.Mode mode)
Return a new "session fail" retry loop. SeeSessionFailRetryLoopfor details on when to use it.- Parameters:
mode- failure mode- Returns:
- new retry loop
-
isConnected
public boolean isConnected()
Returns true if the client is current connected- Returns:
- true/false
-
blockUntilConnectedOrTimedOut
public boolean blockUntilConnectedOrTimedOut() throws java.lang.InterruptedExceptionThis method blocks until the connection to ZK succeeds. Use with caution. The block will timeout after the connection timeout (as passed to the constructor) has elapsed- Returns:
- true if the connection succeeded, false if not
- Throws:
java.lang.InterruptedException- interrupted while waiting
-
start
public void start() throws java.lang.ExceptionMust be called after construction- Throws:
java.io.IOException- errorsjava.lang.Exception
-
close
public void close()
Close the client. Same asclose(int)using the timeout set at construction time.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- See Also:
close(int)
-
close
public void close(int waitForShutdownTimeoutMs)
Close this client object as theclose()method. This method will wait for internal resources to be released.- Parameters:
waitForShutdownTimeoutMs- timeout (in milliseconds) to wait for resources to be released. Use zero or a negative value to skip the wait.
-
setRetryPolicy
public void setRetryPolicy(RetryPolicy policy)
Change the retry policy- Parameters:
policy- new policy
-
getRetryPolicy
public RetryPolicy getRetryPolicy()
Return the current retry policy- Returns:
- policy
-
startTracer
public TimeTrace startTracer(java.lang.String name)
Start a new tracer- Parameters:
name- name of the event- Returns:
- the new tracer (
TimeTrace.commit()must be called)
-
startAdvancedTracer
public OperationTrace startAdvancedTracer(java.lang.String name)
Start a new advanced tracer with more metrics being recorded- Parameters:
name- name of the event- Returns:
- the new tracer (
OperationTrace.commit()must be called)
-
getTracerDriver
public TracerDriver getTracerDriver()
Return the current tracing driver- Returns:
- tracing driver
-
setTracerDriver
public void setTracerDriver(TracerDriver tracer)
Change the tracing driver- Parameters:
tracer- new tracing driver
-
getCurrentConnectionString
public java.lang.String getCurrentConnectionString()
Returns the current known connection string - not guaranteed to be correct value at any point in the future.- Returns:
- connection string
-
getConnectionTimeoutMs
public int getConnectionTimeoutMs()
Return the configured connection timeout- Returns:
- timeout
-
reset
public void reset() throws java.lang.ExceptionFor internal use only - reset the internally managed ZK handle- Throws:
java.lang.Exception- errors
-
getInstanceIndex
public long getInstanceIndex()
Every time a newZooKeeperinstance is allocated, the "instance index" is incremented.- Returns:
- the current instance index
-
getConnectionHandlingPolicy
public ConnectionHandlingPolicy getConnectionHandlingPolicy()
Return the configured connection handling policy- Returns:
- ConnectionHandlingPolicy
-
getLastNegotiatedSessionTimeoutMs
public int getLastNegotiatedSessionTimeoutMs()
Return the most recent value ofZooKeeper.getSessionTimeout()or 0- Returns:
- session timeout or 0
-
internalBlockUntilConnectedOrTimedOut
public void internalBlockUntilConnectedOrTimedOut() throws java.lang.InterruptedExceptionFor internal use only- Throws:
java.lang.InterruptedException- interruptions
-
-