Class SharedCount
- java.lang.Object
-
- org.apache.curator.framework.recipes.shared.SharedCount
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,org.apache.curator.framework.listen.Listenable<SharedCountListener>,SharedCountReader
public class SharedCount extends java.lang.Object implements java.io.Closeable, SharedCountReader, org.apache.curator.framework.listen.Listenable<SharedCountListener>
Manages a shared integer. All clients watching the same path will have the up-to-date value of the shared integer (considering ZK's normal consistency guarantees).
-
-
Constructor Summary
Constructors Modifier Constructor Description SharedCount(org.apache.curator.framework.CuratorFramework client, java.lang.String path, int seedValue)protectedSharedCount(org.apache.curator.framework.CuratorFramework client, java.lang.String path, SharedValue sv)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddListener(SharedCountListener listener)voidaddListener(SharedCountListener listener, java.util.concurrent.Executor executor)voidclose()intgetCount()Return the current value of the countVersionedValue<java.lang.Integer>getVersionedValue()Return the current count and versionvoidremoveListener(SharedCountListener listener)voidsetCount(int newCount)Change the shared count value irrespective of its previous statevoidstart()The shared count must be started before it can be used.booleantrySetCount(int newCount)Deprecated.usetrySetCount(VersionedValue, int)for stronger atomicity guarantees.booleantrySetCount(VersionedValue<java.lang.Integer> previous, int newCount)Changes the shared count only if its value has not changed since the version specified by newCount.
-
-
-
Constructor Detail
-
SharedCount
public SharedCount(org.apache.curator.framework.CuratorFramework client, java.lang.String path, int seedValue)- Parameters:
client- the clientpath- the shared path - i.e. where the shared count is storedseedValue- the initial value for the count if/f the path has not yet been created
-
SharedCount
protected SharedCount(org.apache.curator.framework.CuratorFramework client, java.lang.String path, SharedValue sv)
-
-
Method Detail
-
getCount
public int getCount()
Description copied from interface:SharedCountReaderReturn the current value of the count- Specified by:
getCountin interfaceSharedCountReader- Returns:
- count
-
getVersionedValue
public VersionedValue<java.lang.Integer> getVersionedValue()
Description copied from interface:SharedCountReaderReturn the current count and version- Specified by:
getVersionedValuein interfaceSharedCountReader- Returns:
- count and version
-
setCount
public void setCount(int newCount) throws java.lang.ExceptionChange the shared count value irrespective of its previous state- Parameters:
newCount- new value- Throws:
java.lang.Exception- ZK errors, interruptions, etc.
-
trySetCount
@Deprecated public boolean trySetCount(int newCount) throws java.lang.ExceptionDeprecated.usetrySetCount(VersionedValue, int)for stronger atomicity guarantees. Even if this object's internal state is up-to-date, the caller has no way to ensure that they've read the most recently seen count.Changes the shared count only if its value has not changed since this client last read it. If the count has changed, the value is not set and this client's view of the value is updated. i.e. if the count is not successful you can get the updated value by callinggetCount().- Parameters:
newCount- the new value to attempt- Returns:
- true if the change attempt was successful, false if not. If the change
was not successful,
getCount()will return the updated value - Throws:
java.lang.Exception- ZK errors, interruptions, etc.
-
trySetCount
public boolean trySetCount(VersionedValue<java.lang.Integer> previous, int newCount) throws java.lang.Exception
Changes the shared count only if its value has not changed since the version specified by newCount. If the count has changed, the value is not set and this client's view of the value is updated. i.e. if the count is not successful you can get the updated value by callinggetCount().- Parameters:
newCount- the new value to attempt- Returns:
- true if the change attempt was successful, false if not. If the change
was not successful,
getCount()will return the updated value - Throws:
java.lang.Exception- ZK errors, interruptions, etc.
-
addListener
public void addListener(SharedCountListener listener)
- Specified by:
addListenerin interfaceorg.apache.curator.framework.listen.Listenable<SharedCountListener>
-
addListener
public void addListener(SharedCountListener listener, java.util.concurrent.Executor executor)
- Specified by:
addListenerin interfaceorg.apache.curator.framework.listen.Listenable<SharedCountListener>
-
removeListener
public void removeListener(SharedCountListener listener)
- Specified by:
removeListenerin interfaceorg.apache.curator.framework.listen.Listenable<SharedCountListener>
-
start
public void start() throws java.lang.ExceptionThe shared count must be started before it can be used. Callclose()when you are finished with the shared count- Throws:
java.lang.Exception- ZK errors, interruptions, etc.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
-