public class SharedCount extends Object implements Closeable, SharedCountReader, Listenable<SharedCountListener>
Modifier | Constructor and Description |
---|---|
|
SharedCount(CuratorFramework client,
String path,
int seedValue) |
protected |
SharedCount(CuratorFramework client,
String path,
SharedValue sv) |
Modifier and Type | Method and Description |
---|---|
void |
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 count
|
VersionedValue<Integer> |
getVersionedValue()
Return the current count and version
|
void |
removeListener(SharedCountListener listener)
Remove the given listener
|
void |
setCount(int newCount)
Change the shared count value irrespective of its previous state
|
void |
start()
The shared count must be started before it can be used.
|
boolean |
trySetCount(int newCount)
Deprecated.
use
trySetCount(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. |
boolean |
trySetCount(VersionedValue<Integer> previous,
int newCount)
Changes the shared count only if its value has not changed since the version specified by
newCount.
|
public SharedCount(CuratorFramework client, String path, int seedValue)
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 createdprotected SharedCount(CuratorFramework client, String path, SharedValue sv)
public int getCount()
SharedCountReader
getCount
in interface SharedCountReader
public VersionedValue<Integer> getVersionedValue()
SharedCountReader
getVersionedValue
in interface SharedCountReader
public void setCount(int newCount) throws Exception
newCount
- new valueException
- ZK errors, interruptions, etc.@Deprecated public boolean trySetCount(int newCount) throws Exception
trySetCount(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.getCount()
.newCount
- the new value to attemptgetCount()
will return the updated valueException
- ZK errors, interruptions, etc.public boolean trySetCount(VersionedValue<Integer> previous, int newCount) throws Exception
getCount()
.newCount
- the new value to attemptgetCount()
will return the updated valueException
- ZK errors, interruptions, etc.public void addListener(SharedCountListener listener)
Listenable
addListener
in interface Listenable<SharedCountListener>
listener
- listener to addpublic void addListener(SharedCountListener listener, Executor executor)
Listenable
addListener
in interface Listenable<SharedCountListener>
listener
- listener to addexecutor
- executor to run listener inpublic void removeListener(SharedCountListener listener)
Listenable
removeListener
in interface Listenable<SharedCountListener>
listener
- listener to removepublic void start() throws Exception
close()
when you are
finished with the shared countException
- ZK errors, interruptions, etc.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
Copyright © 2011–2023 The Apache Software Foundation. All rights reserved.