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
ConstructorsModifierConstructorDescriptionSharedValue(CuratorFramework client, String path, byte[] seedValue) protectedSharedValue(WatcherRemoveCuratorFramework client, String path, byte[] seedValue, CuratorWatcher watcher) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Returns the listenablebyte[]getValue()Return the current value of the countVersionedValue<byte[]> Return the current version and valuevoidsetValue(byte[] newValue) Change the shared value irrespective of its previous state.voidstart()The shared value must be started before it can be used.booleantrySetValue(byte[] newValue) Deprecated.booleantrySetValue(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:SharedValueReaderReturn the current value of the count- Specified by:
getValuein interfaceSharedValueReader- Returns:
- count
-
getVersionedValue
Description copied from interface:SharedValueReaderReturn the current version and value- Specified by:
getVersionedValuein interfaceSharedValueReader- Returns:
- version/value
-
setValue
Change the shared value irrespective of its previous state.Later
getValue()will get some more updated value, whether that value is set by this session or a different one cannot be guaranteed though.- 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:
getListenablein 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
trySetValue(VersionedValue, byte[])for stronger atomicity guarantees.