Class SharedCount
java.lang.Object
org.apache.curator.framework.recipes.shared.SharedCount
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Listenable<SharedCountListener>
,SharedCountReader
public class SharedCount
extends Object
implements Closeable, SharedCountReader, 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
ModifierConstructorDescriptionSharedCount
(CuratorFramework client, String path, int seedValue) protected
SharedCount
(CuratorFramework client, String path, SharedValue sv) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(SharedCountListener listener) Add the given listener.void
addListener
(SharedCountListener listener, Executor executor) Add the given listener.void
close()
int
getCount()
Return the current value of the countReturn the current count and versionvoid
removeListener
(SharedCountListener listener) Remove the given listenervoid
setCount
(int newCount) Change the shared count value irrespective of its previous statevoid
start()
The shared count must be started before it can be used.boolean
trySetCount
(int newCount) Deprecated.boolean
trySetCount
(VersionedValue<Integer> previous, int newCount) Changes the shared count only if its value has not changed since the version specified by newCount.
-
Constructor Details
-
SharedCount
- 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
-
-
Method Details
-
getCount
public int getCount()Description copied from interface:SharedCountReader
Return the current value of the count- Specified by:
getCount
in interfaceSharedCountReader
- Returns:
- count
-
getVersionedValue
Description copied from interface:SharedCountReader
Return the current count and version- Specified by:
getVersionedValue
in interfaceSharedCountReader
- Returns:
- count and version
-
setCount
Change the shared count value irrespective of its previous state- Parameters:
newCount
- new value- Throws:
Exception
- ZK errors, interruptions, etc.
-
trySetCount
Deprecated.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:
Exception
- ZK errors, interruptions, etc.
-
start
The shared count must be started before it can be used. Callclose()
when you are finished with the shared count- Throws:
Exception
- ZK errors, interruptions, etc.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
trySetCount(VersionedValue, int)
for stronger atomicity guarantees.