com.netflix.curator.framework.recipes.leader
Class LeaderSelector

java.lang.Object
  extended by com.netflix.curator.framework.recipes.leader.LeaderSelector
All Implemented Interfaces:
Closeable

public class LeaderSelector
extends Object
implements Closeable

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


Constructor Summary
LeaderSelector(CuratorFramework client, String mutexPath, LeaderSelectorListener listener)
           
LeaderSelector(CuratorFramework client, String mutexPath, ThreadFactory threadFactory, Executor executor, LeaderSelectorListener listener)
           
 
Method Summary
 void close()
          Shutdown this selector and remove yourself from the leadership group
 String getId()
          Return the ID that was set via setId(String)
 Participant getLeader()
           Return the id for the current leader.
 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
 void setId(String id)
          Sets the ID to store for this leader.
 void start()
          Attempt leadership.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LeaderSelector

public LeaderSelector(CuratorFramework client,
                      String mutexPath,
                      LeaderSelectorListener listener)
Parameters:
client - the client
mutexPath - the path for this leadership group
listener - listener

LeaderSelector

public LeaderSelector(CuratorFramework client,
                      String mutexPath,
                      ThreadFactory threadFactory,
                      Executor executor,
                      LeaderSelectorListener listener)
Parameters:
client - the client
mutexPath - the path for this leadership group
threadFactory - factory to use for making internal threads
executor - the executor to run in
listener - listener
Method Detail

setId

public void setId(String id)
Sets the ID to store for this leader. Will be the value returned when getParticipants() is called. IMPORTANT: must be called prior to start() to have effect.

Parameters:
id - ID

getId

public String getId()
Return the ID that was set via setId(String)

Returns:
id

start

public void start()
Attempt leadership. This attempt is done in the background - i.e. this method returns immediately. Once you've been assigned leadership you can release it and call this method again to re-obtain leadership


close

public void close()
Shutdown this selector and remove yourself from the leadership group

Specified by:
close in interface Closeable

getParticipants

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.

Returns:
participants
Throws:
Exception - ZK errors, interruptions, etc.

getLeader

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.

Returns:
leader
Throws:
Exception - ZK errors, interruptions, etc.

hasLeadership

public boolean hasLeadership()
Return true if leadership is currently held by this instance

Returns:
true/false


Copyright © 2012. All Rights Reserved.