public class LeaderSelector
extends java.lang.Object
implements java.io.Closeable
Constructor and Description |
---|
LeaderSelector(CuratorFramework client,
java.lang.String leaderPath,
LeaderSelectorListener listener) |
LeaderSelector(CuratorFramework client,
java.lang.String leaderPath,
java.util.concurrent.ThreadFactory threadFactory,
java.util.concurrent.Executor executor,
LeaderSelectorListener listener) |
Modifier and Type | Method and Description |
---|---|
void |
autoRequeue()
By default, when
LeaderSelectorListener.takeLeadership(CuratorFramework) returns, this
instance is not requeued. |
void |
close()
Shutdown this selector and remove yourself from the leadership group
|
java.lang.String |
getId()
Return the ID that was set via
setId(String) |
Participant |
getLeader()
Return the id for the current leader.
|
java.util.Collection<Participant> |
getParticipants()
Returns the set of current participants in the leader selection
|
boolean |
hasLeadership()
Return true if leadership is currently held by this instance
|
boolean |
requeue()
Re-queue an attempt for leadership.
|
void |
setId(java.lang.String id)
Sets the ID to store for this leader.
|
void |
start()
Attempt leadership.
|
public LeaderSelector(CuratorFramework client, java.lang.String leaderPath, LeaderSelectorListener listener)
client
- the clientleaderPath
- the path for this leadership grouplistener
- listenerpublic LeaderSelector(CuratorFramework client, java.lang.String leaderPath, java.util.concurrent.ThreadFactory threadFactory, java.util.concurrent.Executor executor, LeaderSelectorListener listener)
client
- the clientleaderPath
- the path for this leadership groupthreadFactory
- factory to use for making internal threadsexecutor
- the executor to run inlistener
- listenerpublic void autoRequeue()
LeaderSelectorListener.takeLeadership(CuratorFramework)
returns, this
instance is not requeued. Calling this method puts the leader selector into a mode where it
will always requeue itself.public void setId(java.lang.String id)
getParticipants()
is called. IMPORTANT: must be called
prior to start()
to have effect.id
- IDpublic java.lang.String getId()
setId(String)
public void start()
requeue()
for this purpose.public boolean requeue()
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public java.util.Collection<Participant> getParticipants() throws java.lang.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.
java.lang.Exception
- ZK errors, interruptions, etc.public Participant getLeader() throws java.lang.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.
java.lang.Exception
- ZK errors, interruptions, etc.public boolean hasLeadership()