public class LeaderElection extends Object
// Prepare a ZooKeeper instance. ZooKeeper zooKeeper = ... // Prepare a Listener implementation. LeaderElection.LeaderElection.Listener
listener = new LeaderElection.Listener() { @Override public voidonWin
(LeaderElection election) { System.out.println("I'm the leader."); } @Override public voidonLose
(LeaderElection election) { System.out.println("Someone else is the leader."); } @Override public voidonVacant
(LeaderElection election) { System.out.println("The leader resigned. An election will be conducted again."); } @Override public voidonFinish
(LeaderElection election) { System.out.println("The callback chain ended. Not run for election any more."); } @Override public voidonStateChanged
(LeaderElection election,LeaderElection.State
oldState,LeaderElection.State
newState) { System.out.format("The state was changed from %s to %s.\n", oldState, newState); } }; // Conduct a leader election. new LeaderElection() .setZooKeeper
(zooKeeper) .setListener
(listener) .start()
; // Same as above. new LeaderElection() .setZooKeeper
(zooKeeper) .setListener
(listener) .setPath
("/leader") .setId
( String.valueOf(Math.abs(new Random().nextLong())) ) .setAclList
(ZooDefs.Ids.OPEN_ACL_UNSAFE) .start()
;
This implementation repeats to join a leader election, i.e. continues to schedule a callback (and a watcher as necessary), unless it detects either of the following.
- The given
ZooKeeper
instance reportsAUTH_FAILED
orCLOSED
.- This instance is marked as 'shouldFinish' by
finish()
.
LeaderElection.Adapter
is an empty implementation of LeaderElection.Listener
. You may
find it useful when you are interested in only some of the callback methods.
For example, if you are interested in only onStateChanged()
,
using Adapter
will make your code shorter like below.
// Conduct a leader election. new LeaderElection() .setZooKeeper
(zooKeeper) .setListener
(new LeaderElection.LeaderElection.Adapter
() { @Override public voidonStateChanged
(LeaderElection election,LeaderElection.State
oldState,LeaderElection.State
newState) { System.out.format("The state was changed from %s to %s.\n", oldState, newState); } }) .start()
;
Modifier and Type | Class and Description |
---|---|
static class |
LeaderElection.Adapter
An empty implementation of
LeaderElection.Listener . |
static interface |
LeaderElection.Listener
The listener to receive leader election events.
|
static class |
LeaderElection.State
Leader election state.
|
Constructor and Description |
---|
LeaderElection() |
LeaderElection(org.apache.zookeeper.ZooKeeper zooKeeper) |
Modifier and Type | Method and Description |
---|---|
NodeReader |
createReader()
Create a
NodeReader instance to read the content
of the znode that is used for leader election. |
LeaderElection |
finish()
Mark as 'shouldFinish' not to schedule ZooKeeper callbacks
any further.
|
List<org.apache.zookeeper.data.ACL> |
getAclList()
Get the ACL list used for creation of the znode for leader election.
|
List<org.apache.zookeeper.data.ACL> |
getDefaultAclList()
Get the default ACL list (
OPEN_ACL_UNSAFE ). |
String |
getDefaultPath()
Get the default path (
"/leader" ). |
String |
getId()
Get the ID that represents this candidate in leader election.
|
LeaderElection.Listener |
getListener()
Get the listener for leader election events.
|
String |
getPath()
Get the znode path used for leader election.
|
LeaderElection.State |
getState()
Get the current
state . |
org.apache.zookeeper.ZooKeeper |
getZooKeeper()
Get the
ZooKeeper instance used for leader election. |
LeaderElection |
setAclList(List<org.apache.zookeeper.data.ACL> list)
Set the ACL list used for creation of the znode for leader election.
|
LeaderElection |
setId(String id)
Set the ID that represents this candidate in leader election.
|
LeaderElection |
setListener(LeaderElection.Listener listener)
Set the listener for leader election events.
|
LeaderElection |
setPath(String path)
Set the znode path used for leader election.
|
LeaderElection |
setZooKeeper(org.apache.zookeeper.ZooKeeper zooKeeper)
Set the
ZooKeeper instance used for leader election. |
LeaderElection |
start()
Start leader election.
|
public LeaderElection()
public LeaderElection(org.apache.zookeeper.ZooKeeper zooKeeper)
public org.apache.zookeeper.ZooKeeper getZooKeeper()
ZooKeeper
instance used for leader election.ZooKeeper
instance used for leader election.public LeaderElection setZooKeeper(org.apache.zookeeper.ZooKeeper zooKeeper)
ZooKeeper
instance used for leader election.
If no ZooKeeper
instance is set when start()
is called,
an IllegalStateException
is thrown.
zooKeeper
- A ZooKeeper
instance used for leader election.this
object.public String getPath()
public LeaderElection setPath(String path)
If no znode path is set when start()
is called,
the default value, "/leader"
, is used.
path
- The znode path used for leader election.this
object.public String getDefaultPath()
"/leader"
).public String getId()
public LeaderElection setId(String id)
If no ID is set when start()
is called, a random ID
is generated.
id
- The ID that represents this candidate in leader election.this
object.public List<org.apache.zookeeper.data.ACL> getAclList()
public LeaderElection setAclList(List<org.apache.zookeeper.data.ACL> list)
If no ACL list is set when start()
is called, the default list,
ZooDefs.Ids.
OPEN_ACL_UNSAFE
,
is used.
list
- The ACL list used for creation of the znode for leader election.this
object.public List<org.apache.zookeeper.data.ACL> getDefaultAclList()
OPEN_ACL_UNSAFE
).public LeaderElection.Listener getListener()
public LeaderElection setListener(LeaderElection.Listener listener)
listener
- The listener for leader election events.this
object.public LeaderElection start()
This implementation repeats to join a leader election, i.e. continues to schedule a callback (and a watcher as necessary), unless it detects either of the following.
- The given
ZooKeeper
instance reportsAUTH_FAILED
orCLOSED
.- This instance is marked as 'shouldStop' by
finish()
.
this
object.IllegalStateException
- ZooKeeper
instance is set.
CREATED
.
public LeaderElection finish()
finish()
, this LeaderElection
instance never joins a leader election.
Note that calling this method does not remove an existing
Watcher
which is watching the znode for leader
election.
this
object.public LeaderElection.State getState()
state
.public NodeReader createReader()
NodeReader
instance to read the content
of the znode that is used for leader election. This method
behaves almost the same way as the following code.
return newNodeReader()
.setZooKeeper
(getZooKeeper()) .setPath
((getPath() != null) ? getPath() : getDefaultPath());
Note that no listener
is set
to the returned NodeReader
instance.
NodeReader
instance to read the content
of the znode that is used for leader election.Copyright © 2015. All rights reserved.