Class SharedValue
java.lang.Object
org.apache.curator.framework.recipes.shared.SharedValue
- All Implemented Interfaces:
Closeable
,AutoCloseable
,SharedValueReader
Manages a shared value. All clients watching the same path will have the up-to-date
value (considering ZK's normal consistency guarantees).
-
Constructor Summary
ModifierConstructorDescriptionSharedValue
(CuratorFramework client, String path, byte[] seedValue) protected
SharedValue
(WatcherRemoveCuratorFramework client, String path, byte[] seedValue, CuratorWatcher watcher) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Returns the listenablebyte[]
getValue()
Return the current value of the countVersionedValue
<byte[]> Return the current version and valuevoid
setValue
(byte[] newValue) Change the shared value irrespective of its previous statevoid
start()
The shared value must be started before it can be used.boolean
trySetValue
(byte[] newValue) Deprecated.boolean
trySetValue
(VersionedValue<byte[]> previous, byte[] newValue) Changes the shared value only if its value has not changed since the version specified by newValue.
-
Constructor Details
-
SharedValue
- Parameters:
client
- the clientpath
- the shared path - i.e. where the shared value is storedseedValue
- the initial value for the value if/f the path has not yet been created
-
SharedValue
protected SharedValue(WatcherRemoveCuratorFramework client, String path, byte[] seedValue, CuratorWatcher watcher)
-
-
Method Details
-
getValue
public byte[] getValue()Description copied from interface:SharedValueReader
Return the current value of the count- Specified by:
getValue
in interfaceSharedValueReader
- Returns:
- count
-
getVersionedValue
Description copied from interface:SharedValueReader
Return the current version and value- Specified by:
getVersionedValue
in interfaceSharedValueReader
- Returns:
- version/value
-
setValue
Change the shared value irrespective of its previous state- Parameters:
newValue
- new value- Throws:
Exception
- ZK errors, interruptions, etc.
-
trySetValue
Deprecated.usetrySetValue(VersionedValue, byte[])
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 value.Changes the shared value only if its value has not changed since this client last read it. If the value has changed, the value is not set and this client's view of the value is updated. i.e. if the value is not successful you can get the updated value by callinggetValue()
.- Parameters:
newValue
- the new value to attempt- Returns:
- true if the change attempt was successful, false if not. If the change
was not successful,
getValue()
will return the updated value - Throws:
Exception
- ZK errors, interruptions, etc.
-
getListenable
Returns the listenable- Specified by:
getListenable
in interfaceSharedValueReader
- Returns:
- listenable
-
start
The shared value must be started before it can be used. Callclose()
when you are finished with the shared value- Throws:
Exception
- ZK errors, interruptions, etc.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
trySetValue(VersionedValue, byte[])
for stronger atomicity guarantees.