Class ZooKeeperGroup<T extends NodeState>
- java.lang.Object
-
- org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup<T>
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Group<T>
- Direct Known Subclasses:
ZooKeeperMultiGroup
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 Summary
Constructors Constructor 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)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(GroupListener<T> listener)Registers a listener which will be called when the cluster membership changes or the group is connected or disconnected.voidclear()Clears the current data without beginning a new query and without generating any events for listeners.voidclearAndRefresh()Clear out current data and begin a new query on the pathvoidclearAndRefresh(boolean force, boolean sync)Clear out current data and begin a new query on the pathvoidclose()Close/end the cacheprotected voiddoUpdate(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.ChildDatagetCurrentData(String fullPath)Return the current data for the given path.StringgetId()TgetLastState()Gets the last state.org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>>getListenable()Return the cache listenableSequenceComparatorgetSequenceComparator()protected voidhandleException(Throwable e)Default behavior is just to log the exceptionbooleanisConnected()Are we connected with the cluster?booleanisMaster()Check if we are the master.booleanisUnstable()Returns an indication that the sequential, ephemeral node may be registered more than once for this groupTmaster()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)protected voidremove(String fullPath)voidremove(GroupListener<T> listener)Removes a previously added listener.List<T>slaves()Retrieve the list of slaves.voidstart()Start the cache.voidupdate(T state)Update the state of this group member.
-
-
-
Constructor Detail
-
ZooKeeperGroup
public ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, String path, Class<T> clazz)- Parameters:
client- the clientpath- path to watch
-
ZooKeeperGroup
public ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, String path, Class<T> clazz, ThreadFactory threadFactory)- Parameters:
client- the clientpath- path to watchthreadFactory- factory to use when creating internal threads
-
ZooKeeperGroup
public ZooKeeperGroup(org.apache.curator.framework.CuratorFramework client, String path, Class<T> clazz, ExecutorService executorService)- Parameters:
client- the clientpath- path to watchexecutorService- ExecutorService to use for the ZooKeeperGroup's background thread
-
-
Method Detail
-
start
public void start()
Start the cache. The cache is not started automatically. You must call this method.
-
close
public void close() throws IOExceptionClose/end the cache- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceGroup<T extends NodeState>- Throws:
IOException- errors
-
isConnected
public boolean isConnected()
Description copied from interface:GroupAre we connected with the cluster?- Specified by:
isConnectedin interfaceGroup<T extends NodeState>
-
add
public void add(GroupListener<T> listener)
Description copied from interface:GroupRegisters a listener which will be called when the cluster membership changes or the group is connected or disconnected.
-
remove
public void remove(GroupListener<T> listener)
Description copied from interface:GroupRemoves a previously added listener.
-
update
public void update(T state)
Description copied from interface:GroupUpdate the state of this group member. If the state is null, the member will leave the group. This method can be called even if the group is not started, in which case the state will be stored and updated when the group becomes started.
-
members
public Map<String,T> members()
Description copied from interface:GroupGet the list of members connected to this group.
-
isMaster
public boolean isMaster()
Description copied from interface:GroupCheck if we are the master.
-
slaves
public List<T> slaves()
Description copied from interface:GroupRetrieve the list of slaves.
-
getActiveChildren
protected List<ChildData<T>> getActiveChildren()
Filter stale nodes and return only active children from the current data.- Returns:
- list of active children and data
-
getLastState
public T getLastState()
Description copied from interface:GroupGets the last state. This can be used by clients to access that last state, such as when the clients is being added as alistenerbut wants to retrieve the last state to be up to date when the client is added.- Specified by:
getLastStatein interfaceGroup<T extends NodeState>- Returns:
- the state, or null if no last state yet.
-
getSequenceComparator
public SequenceComparator getSequenceComparator()
-
getListenable
public org.apache.curator.framework.listen.ListenerContainer<GroupListener<T>> getListenable()
Return the cache listenable- Returns:
- listenable
-
getCurrentData
public List<ChildData> getCurrentData()
Return the current data. There are no guarantees of accuracy. This is merely the most recent view of the data. The data is returned in sorted order.- Returns:
- list of children and data
-
getCurrentData
public ChildData getCurrentData(String fullPath)
Return the current data for the given path. There are no guarantees of accuracy. This is merely the most recent view of the data. If there is no child with that path,nullis returned.- Parameters:
fullPath- full path to the node to check- Returns:
- data or null
-
clearAndRefresh
public void clearAndRefresh() throws ExceptionClear out current data and begin a new query on the path- Throws:
Exception- errors
-
clearAndRefresh
public void clearAndRefresh(boolean force, boolean sync) throws ExceptionClear out current data and begin a new query on the path- Parameters:
force- - whether to force clear and refresh to trigger updatessync- - whether to run this synchronously (block current thread) or asynchronously- Throws:
Exception- errors
-
clear
public void clear()
Clears the current data without beginning a new query and without generating any events for listeners.
-
handleException
protected void handleException(Throwable e)
Default behavior is just to log the exception- Parameters:
e- the exception
-
remove
protected void remove(String fullPath)
-
members
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
- Throws:
Exception
-
getId
public String getId()
-
isUnstable
public boolean isUnstable()
Returns an indication that the sequential, ephemeral node may be registered more than once for this group
-
-