public class WriteLock extends Object
lock() to
start the process of grabbing the lock; you may get the lock then or it may be
some time later. You can register a listener so that you are invoked
when you get the lock; otherwise you can ask if you have the lock
by calling isOwner()| Modifier and Type | Field and Description |
|---|---|
protected org.apache.zookeeper.ZooKeeper |
zookeeper |
| Constructor and Description |
|---|
WriteLock(org.apache.zookeeper.ZooKeeper zookeeper,
String dir,
List<org.apache.zookeeper.data.ACL> acl)
zookeeper contructor for writelock
|
WriteLock(org.apache.zookeeper.ZooKeeper zookeeper,
String dir,
List<org.apache.zookeeper.data.ACL> acl,
LockListener callback)
zookeeper contructor for writelock with callback
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this strategy and releases any ZooKeeper resources; but keeps the
ZooKeeper instance open
|
protected void |
doClose()
Allow derived classes to perform
some custom closing operations to release resources
|
protected void |
ensureExists(String path,
byte[] data,
List<org.apache.zookeeper.data.ACL> acl,
org.apache.zookeeper.CreateMode flags)
Ensures that the given path exists with the given data, ACL and flags
|
protected void |
ensurePathExists(String path)
Ensures that the given path exists with no data, the current
ACL and no flags
|
List<org.apache.zookeeper.data.ACL> |
getAcl()
return the acl its using
|
String |
getDir()
return the parent dir for lock
|
String |
getId()
return the id for this lock
|
LockListener |
getLockListener()
return the current locklistener
|
long |
getRetryDelay()
get the retry delay in milliseconds
|
org.apache.zookeeper.ZooKeeper |
getZookeeper()
return zookeeper client instance
|
protected boolean |
isClosed()
Returns true if this protocol has been closed
|
boolean |
isOwner()
Returns true if this node is the owner of the
lock (or the leader)
|
boolean |
lock()
Attempts to acquire the exclusive write lock returning whether or not it was
acquired.
|
protected void |
retryDelay(int attemptCount)
Performs a retry delay if this is not the first attempt
|
protected Object |
retryOperation(ZooKeeperOperation operation)
Perform the given operation, retrying if the connection fails
|
void |
setAcl(List<org.apache.zookeeper.data.ACL> acl)
set the acl
|
void |
setLockListener(LockListener callback)
register a different call back listener
|
void |
setRetryDelay(long retryDelay)
Sets the time waited between retry delays
|
void |
unlock()
Removes the lock or associated znode if
you no longer require the lock.
|
public WriteLock(org.apache.zookeeper.ZooKeeper zookeeper,
String dir,
List<org.apache.zookeeper.data.ACL> acl)
zookeeper - zookeeper client instancedir - the parent path you want to use for lockingacls - the acls that you want to use for all the paths,
if null world read/write is used.public WriteLock(org.apache.zookeeper.ZooKeeper zookeeper,
String dir,
List<org.apache.zookeeper.data.ACL> acl,
LockListener callback)
zookeeper - the zookeeper client instancedir - the parent path you want to use for lockingacl - the acls that you want to use for all the pathscallback - the call back instancepublic LockListener getLockListener()
public void setLockListener(LockListener callback)
callback - the call back instancepublic void unlock()
throws RuntimeException
RuntimeException - throws a runtime exception
if it cannot connect to zookeeper.public boolean lock()
throws org.apache.zookeeper.KeeperException,
InterruptedException
org.apache.zookeeper.KeeperExceptionInterruptedExceptionpublic String getDir()
public boolean isOwner()
public String getId()
public void close()
public org.apache.zookeeper.ZooKeeper getZookeeper()
public List<org.apache.zookeeper.data.ACL> getAcl()
public void setAcl(List<org.apache.zookeeper.data.ACL> acl)
acl - the acl to set topublic long getRetryDelay()
public void setRetryDelay(long retryDelay)
retryDelay - the retry delayprotected void doClose()
protected Object retryOperation(ZooKeeperOperation operation) throws org.apache.zookeeper.KeeperException, InterruptedException
org.apache.zookeeper.KeeperExceptionInterruptedExceptionprotected void ensurePathExists(String path)
path - protected void ensureExists(String path, byte[] data, List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode flags)
path - acl - flags - protected boolean isClosed()
protected void retryDelay(int attemptCount)
attemptCount - the number of the attempts performed so farCopyright © 2015–2016 Baidu. All rights reserved.