@InterfaceAudience.Public public class EntityLock extends Object
Use LockServiceClient to build instances. Then call requestLock().
requestLock() will contact master to queue the lock and start the heartbeat thread
which will check lock's status periodically and once the lock is acquired, it will send the
heartbeats to the master.
Use await(long, java.util.concurrent.TimeUnit) or await(long, TimeUnit) to wait for the lock to be acquired.
Always call unlock() irrespective of whether lock was acquired or not. If the lock
was acquired, it'll be released. If it was not acquired, it is possible that master grants the
lock in future and the heartbeat thread keeps it alive forever by sending heartbeats.
Calling unlock() will stop the heartbeat thread and cancel the lock queued on master.
There are 4 ways in which these remote locks may be released/can be lost:
unlock().
EntityLock lock = lockServiceClient.*Lock(...., "exampled lock", abortable);
lock.requestLock();
....
....can do other initializations here since lock is 'asynchronous'...
....
if (lock.await(timeout)) {
....logic requiring mutual exclusion
}
lock.unlock();
| 限定符和类型 | 类和说明 |
|---|---|
protected class |
EntityLock.LockHeartbeatWorker |
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
HEARTBEAT_TIME_BUFFER |
public boolean isLocked()
public void requestLock()
throws IOException
await() to wait on lock.
Always call unlock() after calling the below, even after error.IOExceptionpublic boolean await(long timeout,
TimeUnit timeUnit)
throws InterruptedException
timeout - in milliseconds. If set to 0, waits indefinitely.InterruptedExceptionpublic void await()
throws InterruptedException
public void unlock()
throws IOException
IOExceptionCopyright © 2007–2019 The Apache Software Foundation. All rights reserved.