|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.apache.curator.framework.recipes.leader.LeaderLatch
public class LeaderLatch
Abstraction to select a "leader" amongst multiple contenders in a group of JMVs connected to a Zookeeper cluster. If a group of N thread/processes contend for leadership one will randomly be assigned leader until it releases leadership at which time another one from the group will randomly be chosen
| Nested Class Summary | |
|---|---|
static class |
LeaderLatch.CloseMode
How to handle listeners when the latch is closed |
static class |
LeaderLatch.State
|
| Constructor Summary | |
|---|---|
LeaderLatch(org.apache.curator.framework.CuratorFramework client,
String latchPath)
|
|
LeaderLatch(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String id)
|
|
LeaderLatch(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String id,
LeaderLatch.CloseMode closeMode)
|
|
| Method Summary | |
|---|---|
void |
addListener(LeaderLatchListener listener)
Attaches a listener to this LeaderLatch Attaching the same listener multiple times is a noop from the second time on. |
void |
addListener(LeaderLatchListener listener,
Executor executor)
Attaches a listener to this LeaderLatch Attaching the same listener multiple times is a noop from the second time on. |
void |
await()
Causes the current thread to wait until this instance acquires leadership unless the thread is interrupted or closed. |
boolean |
await(long timeout,
TimeUnit unit)
Causes the current thread to wait until this instance acquires leadership unless the thread is interrupted, the specified waiting time elapses or the instance is closed. |
protected boolean |
cancelStartTask()
|
void |
close()
Remove this instance from the leadership election. |
void |
close(LeaderLatch.CloseMode closeMode)
Remove this instance from the leadership election. |
String |
getId()
Return this instance's participant Id |
Participant |
getLeader()
Return the id for the current leader. |
Collection<Participant> |
getParticipants()
Returns the set of current participants in the leader selection |
LeaderLatch.State |
getState()
Returns this instances current state, this is the only way to verify that the object has been closed before closing again. |
boolean |
hasLeadership()
Return true if leadership is currently held by this instance |
void |
removeListener(LeaderLatchListener listener)
Removes a given listener from this LeaderLatch |
void |
start()
Add this instance to the leadership election and attempt to acquire leadership. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public LeaderLatch(org.apache.curator.framework.CuratorFramework client,
String latchPath)
client - the clientlatchPath - the path for this leadership group
public LeaderLatch(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String id)
client - the clientlatchPath - the path for this leadership groupid - participant ID
public LeaderLatch(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String id,
LeaderLatch.CloseMode closeMode)
client - the clientlatchPath - the path for this leadership groupid - participant IDcloseMode - behaviour of listener on explicit close.| Method Detail |
|---|
public void start()
throws Exception
Exception - errors
public void close()
throws IOException
close in interface CloseableIOException - errors
public void close(LeaderLatch.CloseMode closeMode)
throws IOException
closeMode - allows the default close mode to be overridden at the time the latch is closed.
IOException - errorsprotected boolean cancelStartTask()
public void addListener(LeaderLatchListener listener)
listener - the listener to attach
public void addListener(LeaderLatchListener listener,
Executor executor)
listener - the listener to attachexecutor - An executor to run the methods for the listener on.public void removeListener(LeaderLatchListener listener)
listener - the listener to remove
public void await()
throws InterruptedException,
EOFException
Causes the current thread to wait until this instance acquires leadership unless the thread is interrupted or closed.
If this instance already is the leader then this method returns immediately.
Otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happen:
If the current thread:
then InterruptedException is thrown and the current thread's
interrupted status is cleared.
InterruptedException - if the current thread is interrupted
while waiting
EOFException - if the instance is closed
while waiting
public boolean await(long timeout,
TimeUnit unit)
throws InterruptedException
Causes the current thread to wait until this instance acquires leadership unless the thread is interrupted, the specified waiting time elapses or the instance is closed.
If this instance already is the leader then this method returns immediately
with the value true.
Otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of four things happen:
If the current thread:
then InterruptedException is thrown and the current thread's
interrupted status is cleared.
If the specified waiting time elapses or the instance is closed
then the value false is returned. If the time is less than or equal to zero, the method
will not wait at all.
timeout - the maximum time to waitunit - the time unit of the timeout argument
true if the count reached zero and false
if the waiting time elapsed before the count reached zero or the instances was closed
InterruptedException - if the current thread is interrupted
while waitingpublic String getId()
public LeaderLatch.State getState()
public Collection<Participant> getParticipants()
throws Exception
Returns the set of current participants in the leader selection
NOTE - this method polls the ZK server. Therefore it can possibly
return a value that does not match hasLeadership() as hasLeadership
uses a local field of the class.
Exception - ZK errors, interruptions, etc.
public Participant getLeader()
throws Exception
Return the id for the current leader. If for some reason there is no current leader, a dummy participant is returned.
NOTE - this method polls the ZK server. Therefore it can possibly
return a value that does not match hasLeadership() as hasLeadership
uses a local field of the class.
Exception - ZK errors, interruptions, etc.public boolean hasLeadership()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||