public class ZooKeeperLeaderElectionService extends Object implements LeaderElectionService, org.apache.curator.framework.recipes.leader.LeaderLatchListener, org.apache.curator.framework.recipes.cache.NodeCacheListener, org.apache.curator.framework.api.UnhandledErrorListener
| Constructor and Description |
|---|
ZooKeeperLeaderElectionService(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String leaderPath)
Creates a ZooKeeperLeaderElectionService object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
confirmLeadership(UUID leaderSessionID,
String leaderAddress)
Confirms that the
LeaderContender has accepted the leadership identified by the
given leader session id. |
UUID |
getLeaderSessionID()
Returns the current leader session ID or null, if the contender is not the leader.
|
protected void |
handleStateChange(org.apache.curator.framework.state.ConnectionState newState) |
boolean |
hasLeadership(UUID leaderSessionId)
Returns true if the
LeaderContender with which the service has been started owns
currently the leadership under the given leader session id. |
void |
isLeader() |
void |
nodeChanged() |
void |
notLeader() |
void |
start(LeaderContender contender)
Starts the leader election service.
|
void |
stop()
Stops the leader election service.
|
String |
toString() |
void |
unhandledError(String message,
Throwable e) |
protected void |
writeLeaderInformation()
Writes the current leader's address as well the given leader session ID to ZooKeeper.
|
public ZooKeeperLeaderElectionService(org.apache.curator.framework.CuratorFramework client,
String latchPath,
String leaderPath)
client - Client which is connected to the ZooKeeper quorumlatchPath - ZooKeeper node path for the leader election latchleaderPath - ZooKeeper node path for the node which stores the current leader informationpublic UUID getLeaderSessionID()
public void start(LeaderContender contender) throws Exception
LeaderElectionServicestart in interface LeaderElectionServicecontender - LeaderContender which applies for the leadershipExceptionpublic void stop()
throws Exception
LeaderElectionServicestop in interface LeaderElectionServiceExceptionpublic void confirmLeadership(UUID leaderSessionID, String leaderAddress)
LeaderElectionServiceLeaderContender has accepted the leadership identified by the
given leader session id. It also publishes the leader address under which the leader is
reachable.
The rational behind this method is to establish an order between setting the new leader
session ID in the LeaderContender and publishing the new leader session ID as well
as the leader address to the leader retrieval services.
confirmLeadership in interface LeaderElectionServiceleaderSessionID - The new leader session IDleaderAddress - The address of the new leaderpublic boolean hasLeadership(@Nonnull UUID leaderSessionId)
LeaderElectionServiceLeaderContender with which the service has been started owns
currently the leadership under the given leader session id.hasLeadership in interface LeaderElectionServiceleaderSessionId - identifying the current leaderLeaderContender is the leader, otherwise falsepublic void isLeader()
isLeader in interface org.apache.curator.framework.recipes.leader.LeaderLatchListenerpublic void notLeader()
notLeader in interface org.apache.curator.framework.recipes.leader.LeaderLatchListenerpublic void nodeChanged()
throws Exception
nodeChanged in interface org.apache.curator.framework.recipes.cache.NodeCacheListenerExceptionprotected void writeLeaderInformation()
protected void handleStateChange(org.apache.curator.framework.state.ConnectionState newState)
public void unhandledError(String message, Throwable e)
unhandledError in interface org.apache.curator.framework.api.UnhandledErrorListenerCopyright © 2014–2020 The Apache Software Foundation. All rights reserved.