Class ZooKeeperRetry
- java.lang.Object
-
- org.apache.zookeeper.ZooKeeper
-
- org.apache.zookeeper.retry.ZooKeeperRetry
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class ZooKeeperRetry extends ZooKeeper
A Class which extendsZooKeeper
and will automatically retry calls to zookeeper if aKeeperException.ConnectionLossException
occurs.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.zookeeper.ZooKeeper
ZooKeeper.States, ZooKeeper.WatchRegistration
-
-
Field Summary
-
Fields inherited from class org.apache.zookeeper.ZooKeeper
cnxn, hostProvider, SECURE_CLIENT, ZOOKEEPER_CLIENT_CNXN_SOCKET
-
-
Constructor Summary
Constructors Constructor Description ZooKeeperRetry(java.lang.String connectString, int sessionTimeout, Watcher watcher)
ZooKeeperRetry(java.lang.String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close this client object.java.lang.String
create(java.lang.String path, byte[] data, java.util.List<ACL> acl, CreateMode createMode)
Create a node with the given path.void
delete(java.lang.String path, int version)
Delete the node with the given path.Stat
exists(java.lang.String path, boolean watch)
Return the stat of the node of the given path.Stat
exists(java.lang.String path, Watcher watcher)
Return the stat of the node of the given path.java.util.List<ACL>
getACL(java.lang.String path, Stat stat)
Return the ACL and stat of the node of the given path.java.util.List<java.lang.String>
getChildren(java.lang.String path, boolean watch)
Return the list of the children of the node of the given path.java.util.List<java.lang.String>
getChildren(java.lang.String path, Watcher watcher)
Return the list of the children of the node of the given path.byte[]
getData(java.lang.String path, boolean watch, Stat stat)
Return the data and the stat of the node of the given path.byte[]
getData(java.lang.String path, Watcher watcher, Stat stat)
Return the data and the stat of the node of the given path.Stat
setACL(java.lang.String path, java.util.List<ACL> acl, int aclVersion)
Set the ACL for the node of the given path if such a node exists and the given aclVersion matches the acl version of the node.Stat
setData(java.lang.String path, byte[] data, int version)
Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions).void
setRetryLimit(int limit)
boolean
testConnection()
-
Methods inherited from class org.apache.zookeeper.ZooKeeper
addAuthInfo, addWatch, addWatch, addWatch, addWatch, close, create, create, create, create, create, delete, exists, exists, getACL, getAllChildrenNumber, getAllChildrenNumber, getChildren, getChildren, getChildren, getChildren, getChildren, getChildren, getChildWatches, getClientConfig, getConfig, getConfig, getConfig, getConfig, getData, getData, getDataWatches, getEphemerals, getEphemerals, getEphemerals, getEphemerals, getExistWatches, getPersistentRecursiveWatches, getPersistentWatches, getSaslClient, getSessionId, getSessionPasswd, getSessionTimeout, getState, getTestable, multi, multi, multiInternal, multiInternal, register, removeAllWatches, removeAllWatches, removeWatches, removeWatches, setACL, setData, sync, testableLocalSocketAddress, testableRemoteSocketAddress, testableWaitForShutdown, toString, transaction, updateServerList, whoAmI
-
-
-
-
Constructor Detail
-
ZooKeeperRetry
public ZooKeeperRetry(java.lang.String connectString, int sessionTimeout, Watcher watcher) throws java.io.IOException
- Parameters:
connectString
-sessionTimeout
-watcher
-- Throws:
java.io.IOException
-
ZooKeeperRetry
public ZooKeeperRetry(java.lang.String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) throws java.io.IOException
- Parameters:
connectString
-sessionTimeout
-watcher
-sessionId
-sessionPasswd
-- Throws:
java.io.IOException
-
-
Method Detail
-
close
public void close() throws java.lang.InterruptedException
Description copied from class:ZooKeeper
Close this client object. Once the client is closed, its session becomes invalid. All the ephemeral nodes in the ZooKeeper server associated with the session will be removed. The watches left on those nodes (and on their parents) will be triggered.Added in 3.5.3: try-with-resources may be used instead of calling close directly.
This method does not wait for all internal threads to exit. Use the
ZooKeeper.close(int)
method to wait for all resources to be released
-
create
public java.lang.String create(java.lang.String path, byte[] data, java.util.List<ACL> acl, CreateMode createMode) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Create a node with the given path. The node data will be the given data, and node acl will be the given acl.The flags argument specifies whether the created node will be ephemeral or not.
An ephemeral node will be removed by the ZooKeeper automatically when the session associated with the creation of the node expires.
The flags argument can also specify to create a sequential node. The actual path name of a sequential node will be the given path plus a suffix "i" where i is the current sequential number of the node. The sequence number is always fixed length of 10 digits, 0 padded. Once such a node is created, the sequential number will be incremented by one.
If a node with the same actual path already exists in the ZooKeeper, a KeeperException with error code KeeperException.NodeExists will be thrown. Note that since a different actual path is used for each invocation of creating sequential node with the same path argument, the call will never throw "file exists" KeeperException.
If the parent node does not exist in the ZooKeeper, a KeeperException with error code KeeperException.NoNode will be thrown.
An ephemeral node cannot have children. If the parent node of the given path is ephemeral, a KeeperException with error code KeeperException.NoChildrenForEphemerals will be thrown.
This operation, if successful, will trigger all the watches left on the node of the given path by exists and getData API calls, and the watches left on the parent node by getChildren API calls.
If a node is created successfully, the ZooKeeper server will trigger the watches on the path left by exists calls, and the watches on the parent of the node by getChildren calls.
The maximum allowable size of the data array is 1 MB (1,048,576 bytes). Arrays larger than this will cause a KeeperExecption to be thrown.
- Overrides:
create
in classZooKeeper
- Parameters:
path
- the path for the nodedata
- the initial data for the nodeacl
- the acl for the nodecreateMode
- specifying whether the node to be created is ephemeral and/or sequential- Returns:
- the actual path of the created node
- Throws:
KeeperException
- if the server returns a non-zero error codeKeeperException.InvalidACLException
- if the ACL is invalid, null, or emptyjava.lang.InterruptedException
- if the transaction is interrupted
-
delete
public void delete(java.lang.String path, int version) throws java.lang.InterruptedException, KeeperException
Description copied from class:ZooKeeper
Delete the node with the given path. The call will succeed if such a node exists, and the given version matches the node's version (if the given version is -1, it matches any node's versions).A KeeperException with error code KeeperException.NoNode will be thrown if the nodes does not exist.
A KeeperException with error code KeeperException.BadVersion will be thrown if the given version does not match the node's version.
A KeeperException with error code KeeperException.NotEmpty will be thrown if the node has children.
This operation, if successful, will trigger all the watches on the node of the given path left by exists API calls, and the watches on the parent node left by getChildren API calls.
- Overrides:
delete
in classZooKeeper
- Parameters:
path
- the path of the node to be deleted.version
- the expected node version.- Throws:
java.lang.InterruptedException
- IF the server transaction is interruptedKeeperException
- If the server signals an error with a non-zero return code.
-
exists
public Stat exists(java.lang.String path, boolean watch) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the stat of the node of the given path. Return null if no such a node exists.If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
- Overrides:
exists
in classZooKeeper
- Parameters:
path
- the node pathwatch
- whether need to watch this node- Returns:
- the stat of the node of the given path; return null if no such a node exists.
- Throws:
KeeperException
- If the server signals an errorjava.lang.InterruptedException
- If the server transaction is interrupted.
-
exists
public Stat exists(java.lang.String path, Watcher watcher) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the stat of the node of the given path. Return null if no such a node exists.If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the data on the node.
- Overrides:
exists
in classZooKeeper
- Parameters:
path
- the node pathwatcher
- explicit watcher- Returns:
- the stat of the node of the given path; return null if no such a node exists.
- Throws:
KeeperException
- If the server signals an errorjava.lang.InterruptedException
- If the server transaction is interrupted.
-
getACL
public java.util.List<ACL> getACL(java.lang.String path, Stat stat) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the ACL and stat of the node of the given path.A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
- Overrides:
getACL
in classZooKeeper
- Parameters:
path
- the given path for the nodestat
- the stat of the node will be copied to this parameter if not null.- Returns:
- the ACL array of the given node.
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.java.lang.InterruptedException
- If the server transaction is interrupted.
-
getChildren
public java.util.List<java.lang.String> getChildren(java.lang.String path, boolean watch) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the list of the children of the node of the given path.If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that deletes the node of the given path or creates/delete a child under the node.
The list of children returned is not sorted and no guarantee is provided as to its natural or lexical order.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
- Overrides:
getChildren
in classZooKeeper
- Parameters:
path
- the node pathwatch
- whether need to watch this node- Returns:
- an unordered array of children of the node with the given path
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.java.lang.InterruptedException
- If the server transaction is interrupted.
-
getChildren
public java.util.List<java.lang.String> getChildren(java.lang.String path, Watcher watcher) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the list of the children of the node of the given path.If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that deletes the node of the given path or creates/delete a child under the node.
The list of children returned is not sorted and no guarantee is provided as to its natural or lexical order.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
- Overrides:
getChildren
in classZooKeeper
watcher
- explicit watcher- Returns:
- an unordered array of children of the node with the given path
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.java.lang.InterruptedException
- If the server transaction is interrupted.
-
getData
public byte[] getData(java.lang.String path, boolean watch, Stat stat) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the data and the stat of the node of the given path.If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
- Overrides:
getData
in classZooKeeper
- Parameters:
path
- the given pathwatch
- whether need to watch this nodestat
- the stat of the node- Returns:
- the data of the node
- Throws:
KeeperException
- If the server signals an error with a non-zero error codejava.lang.InterruptedException
- If the server transaction is interrupted.
-
getData
public byte[] getData(java.lang.String path, Watcher watcher, Stat stat) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Return the data and the stat of the node of the given path.If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node with the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the node.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
- Overrides:
getData
in classZooKeeper
- Parameters:
path
- the given pathwatcher
- explicit watcherstat
- the stat of the node- Returns:
- the data of the node
- Throws:
KeeperException
- If the server signals an error with a non-zero error codejava.lang.InterruptedException
- If the server transaction is interrupted.
-
setACL
public Stat setACL(java.lang.String path, java.util.List<ACL> acl, int aclVersion) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Set the ACL for the node of the given path if such a node exists and the given aclVersion matches the acl version of the node. Return the stat of the node.A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
A KeeperException with error code KeeperException.BadVersion will be thrown if the given aclVersion does not match the node's aclVersion.
- Overrides:
setACL
in classZooKeeper
- Parameters:
path
- the given path for the nodeacl
- the given acl for the nodeaclVersion
- the given acl version of the node- Returns:
- the stat of the node.
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.KeeperException.InvalidACLException
- If the acl is invalide.java.lang.InterruptedException
- If the server transaction is interrupted.
-
setData
public Stat setData(java.lang.String path, byte[] data, int version) throws KeeperException, java.lang.InterruptedException
Description copied from class:ZooKeeper
Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions). Return the stat of the node.This operation, if successful, will trigger all the watches on the node of the given path left by getData calls.
A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists.
A KeeperException with error code KeeperException.BadVersion will be thrown if the given version does not match the node's version.
The maximum allowable size of the data array is 1 MB (1,048,576 bytes). Arrays larger than this will cause a KeeperException to be thrown.
- Overrides:
setData
in classZooKeeper
- Parameters:
path
- the path of the nodedata
- the data to setversion
- the expected matching version- Returns:
- the state of the node
- Throws:
KeeperException
- If the server signals an error with a non-zero error code.java.lang.InterruptedException
- If the server transaction is interrupted.
-
setRetryLimit
public void setRetryLimit(int limit)
- Parameters:
limit
-
-
testConnection
public boolean testConnection()
- Returns:
- true if successfully connected to zookeeper
-
-