public class ZooKeeperLeaderElectionService extends Object implements LeaderElectionService, org.apache.curator.framework.recipes.leader.LeaderLatchListener, org.apache.curator.framework.recipes.cache.NodeCacheListener
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 |
confirmLeaderSessionID(UUID leaderSessionID)
Confirms that the new leader session ID has been successfully received by the new leader.
|
UUID |
getLeaderSessionID()
Returns the current leader session ID or null, if the contender is not the leader.
|
boolean |
hasLeadership()
Returns true if the
LeaderContender with which the service has been started owns
currently the leadership. |
void |
isLeader() |
void |
nodeChanged() |
void |
notLeader() |
void |
start(LeaderContender contender)
Starts the leader election service.
|
void |
stop()
Stops the leader election service.
|
protected void |
writeLeaderInformation(UUID leaderSessionID)
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
LeaderElectionService
start
in interface LeaderElectionService
contender
- LeaderContender which applies for the leadershipException
public void stop() throws Exception
LeaderElectionService
stop
in interface LeaderElectionService
Exception
public void confirmLeaderSessionID(UUID leaderSessionID)
LeaderElectionService
LeaderContender
.
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 to the
leader retrieval services.confirmLeaderSessionID
in interface LeaderElectionService
leaderSessionID
- The new leader session IDpublic boolean hasLeadership()
LeaderElectionService
LeaderContender
with which the service has been started owns
currently the leadership.hasLeadership
in interface LeaderElectionService
LeaderContender
is the leader, otherwise falsepublic void isLeader()
isLeader
in interface org.apache.curator.framework.recipes.leader.LeaderLatchListener
public void notLeader()
notLeader
in interface org.apache.curator.framework.recipes.leader.LeaderLatchListener
public void nodeChanged() throws Exception
nodeChanged
in interface org.apache.curator.framework.recipes.cache.NodeCacheListener
Exception
protected void writeLeaderInformation(UUID leaderSessionID)
leaderSessionID
- Leader session ID which is written to ZooKeeperCopyright © 2014–2017 The Apache Software Foundation. All rights reserved.