public class ZooKeeperGroup<T extends NodeState> extends Object implements Group<T>
A utility that attempts to keep all data from all children of a ZK path locally cached. This class will watch the ZK path, respond to update/create/delete events, pull down the data, etc. You can register a listener that will get notified when changes occur.
IMPORTANT - it's not possible to stay transactionally in sync. Users of this class must be prepared for false-positives and false-negatives. Additionally, always use the version number when updating data to avoid overwriting another process' change.
Constructor and Description |
---|
ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client,
String path,
Class<T> clazz) |
ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client,
String path,
Class<T> clazz,
ExecutorService executorService) |
ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client,
String path,
Class<T> clazz,
ThreadFactory threadFactory) |
Modifier and Type | Method and Description |
---|---|
void |
add(GroupListener<T> listener)
Registers a listener which will be called
when the cluster membership changes or
the group is connected or disconnected.
|
void |
clear()
Clears the current data without beginning a new query and without generating any events
for listeners.
|
void |
clearAndRefresh()
Clear out current data and begin a new query on the path
|
void |
clearAndRefresh(boolean force,
boolean sync)
Clear out current data and begin a new query on the path
|
void |
close()
Close/end the cache
|
protected void |
doUpdate(T state) |
protected List<ChildData<T>> |
getActiveChildren()
Filter stale nodes and return only active children from the current data.
|
List<ChildData> |
getCurrentData()
Return the current data.
|
ChildData |
getCurrentData(String fullPath)
Return the current data for the given path.
|
String |
getId() |
T |
getLastState()
Gets the last state.
|
org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>> |
getListenable()
Return the cache listenable
|
SequenceComparator |
getSequenceComparator() |
protected void |
handleException(Throwable e)
Default behavior is just to log the exception
|
boolean |
isConnected()
Are we connected with the cluster?
|
boolean |
isMaster()
Check if we are the master.
|
boolean |
isUnstable()
Returns an indication that the sequential, ephemeral node may be registered more than once for this group
|
T |
master()
Retrieve the master node.
|
Map<String,T> |
members()
Get the list of members connected to this group.
|
static <T> Map<String,T> |
members(com.fasterxml.jackson.databind.ObjectMapper mapper,
org.apache.curator.framework.CuratorFramework curator,
String path,
Class<T> clazz) |
void |
remove(GroupListener<T> listener)
Removes a previously added listener.
|
protected void |
remove(String fullPath) |
List<T> |
slaves()
Retrieve the list of slaves.
|
void |
start()
Start the cache.
|
void |
update(T state)
Update the state of this group member.
|
public ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, String path, Class<T> clazz)
client
- the clientpath
- path to watchpublic ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, String path, Class<T> clazz, ThreadFactory threadFactory)
client
- the clientpath
- path to watchthreadFactory
- factory to use when creating internal threadspublic ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, String path, Class<T> clazz, ExecutorService executorService)
client
- the clientpath
- path to watchexecutorService
- ExecutorService to use for the ZooKeeperGroup's background threadpublic void start()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in interface Group<T extends NodeState>
IOException
- errorspublic boolean isConnected()
Group
isConnected
in interface Group<T extends NodeState>
public void add(GroupListener<T> listener)
Group
public void remove(GroupListener<T> listener)
Group
public void update(T state)
Group
public Map<String,T> members()
Group
public boolean isMaster()
Group
public List<T> slaves()
Group
protected List<ChildData<T>> getActiveChildren()
public T getLastState()
Group
listener
but wants to retrieve the last state to be up to date when the
client is added.getLastState
in interface Group<T extends NodeState>
public SequenceComparator getSequenceComparator()
public org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>> getListenable()
public List<ChildData> getCurrentData()
public ChildData getCurrentData(String fullPath)
null
is returned.fullPath
- full path to the node to checkpublic void clearAndRefresh() throws Exception
Exception
- errorspublic void clearAndRefresh(boolean force, boolean sync) throws Exception
force
- - whether to force clear and refresh to trigger updatessync
- - whether to run this synchronously (block current thread) or asynchronouslyException
- errorspublic void clear()
protected void handleException(Throwable e)
e
- the exceptionprotected void remove(String fullPath)
public static <T> Map<String,T> members(com.fasterxml.jackson.databind.ObjectMapper mapper, org.apache.curator.framework.CuratorFramework curator, String path, Class<T> clazz) throws Exception
Exception
public String getId()
public boolean isUnstable()
Apache Camel