public class ZooKeeperGroup<T extends NodeState> extends java.lang.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.
Modifier and Type | Field and Description |
---|---|
protected java.util.concurrent.ConcurrentMap<java.lang.String,ChildData<T>> |
currentData |
static org.codehaus.jackson.map.ObjectMapper |
MAPPER |
protected SequenceComparator |
sequenceComparator |
Constructor and Description |
---|
ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client,
java.lang.String path,
java.lang.Class<T> clazz) |
ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client,
java.lang.String path,
java.lang.Class<T> clazz,
java.util.concurrent.ExecutorService executorService) |
ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client,
java.lang.String path,
java.lang.Class<T> clazz,
java.util.concurrent.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 |
close()
Close/end the cache
|
protected void |
doUpdate(T state) |
java.util.List<ChildData> |
getCurrentData()
Return the current data.
|
ChildData |
getCurrentData(java.lang.String fullPath)
Return the current data for the given path.
|
java.lang.String |
getId() |
org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>> |
getListenable()
Return the cache listenable
|
protected void |
handleException(java.lang.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.
|
T |
master()
Retrieve the master node.
|
java.util.Map<java.lang.String,T> |
members()
Get the list of members connected to this group.
|
static <T> java.util.Map<java.lang.String,T> |
members(org.apache.curator.framework.CuratorFramework curator,
java.lang.String path,
java.lang.Class<T> clazz) |
void |
remove(GroupListener<T> listener)
Removes a previously added listener.
|
protected void |
remove(java.lang.String fullPath) |
java.util.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 static final org.codehaus.jackson.map.ObjectMapper MAPPER
protected final java.util.concurrent.ConcurrentMap<java.lang.String,ChildData<T extends NodeState>> currentData
protected final SequenceComparator sequenceComparator
public ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, java.lang.String path, java.lang.Class<T> clazz)
client
- the clientpath
- path to watchpublic ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, java.lang.String path, java.lang.Class<T> clazz, java.util.concurrent.ThreadFactory threadFactory)
client
- the clientpath
- path to watchthreadFactory
- factory to use when creating internal threadspublic ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, java.lang.String path, java.lang.Class<T> clazz, java.util.concurrent.ExecutorService executorService)
client
- the clientpath
- path to watchexecutorService
- ExecutorService to use for the ZooKeeperGroup's background threadpublic void start()
public void close() throws java.io.IOException
public 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
protected void doUpdate(T state) throws java.lang.Exception
java.lang.Exception
public java.util.Map<java.lang.String,T> members()
Group
public boolean isMaster()
Group
public java.util.List<T> slaves()
Group
public org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>> getListenable()
public java.util.List<ChildData> getCurrentData()
public ChildData getCurrentData(java.lang.String fullPath)
null
is returned.fullPath
- full path to the node to checkpublic void clearAndRefresh() throws java.lang.Exception
java.lang.Exception
- errorspublic void clear()
protected void handleException(java.lang.Throwable e)
e
- the exceptionprotected void remove(java.lang.String fullPath)
public static <T> java.util.Map<java.lang.String,T> members(org.apache.curator.framework.CuratorFramework curator, java.lang.String path, java.lang.Class<T> clazz) throws java.lang.Exception
java.lang.Exception
public java.lang.String getId()
Copyright © 2014 Red Hat. All Rights Reserved.