Class SharedValue
- java.lang.Object
-
- org.apache.curator.framework.recipes.shared.SharedValue
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,SharedValueReader
public class SharedValue extends java.lang.Object implements java.io.Closeable, 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
Constructors Modifier Constructor Description SharedValue(org.apache.curator.framework.CuratorFramework client, java.lang.String path, byte[] seedValue)protectedSharedValue(org.apache.curator.framework.WatcherRemoveCuratorFramework client, java.lang.String path, byte[] seedValue, org.apache.curator.framework.api.CuratorWatcher watcher)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()org.apache.curator.framework.listen.ListenerContainer<SharedValueListener>getListenable()Returns the listenablebyte[]getValue()Return the current value of the countVersionedValue<byte[]>getVersionedValue()Return the current version and valuevoidsetValue(byte[] newValue)Change the shared value value irrespective of its previous statevoidstart()The shared value must be started before it can be used.booleantrySetValue(byte[] newValue)Deprecated.usetrySetValue(VersionedValue, byte[])for stronger atomicity guarantees.booleantrySetValue(VersionedValue<byte[]> previous, byte[] newValue)Changes the shared value only if its value has not changed since the version specified by newValue.
-
-
-
Constructor Detail
-
SharedValue
public SharedValue(org.apache.curator.framework.CuratorFramework client, java.lang.String path, byte[] seedValue)- 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(org.apache.curator.framework.WatcherRemoveCuratorFramework client, java.lang.String path, byte[] seedValue, org.apache.curator.framework.api.CuratorWatcher watcher)
-
-
Method Detail
-
getValue
public byte[] getValue()
Description copied from interface:SharedValueReaderReturn the current value of the count- Specified by:
getValuein interfaceSharedValueReader- Returns:
- count
-
getVersionedValue
public VersionedValue<byte[]> getVersionedValue()
Description copied from interface:SharedValueReaderReturn the current version and value- Specified by:
getVersionedValuein interfaceSharedValueReader- Returns:
- version/value
-
setValue
public void setValue(byte[] newValue) throws java.lang.ExceptionChange the shared value value irrespective of its previous state- Parameters:
newValue- new value- Throws:
java.lang.Exception- ZK errors, interruptions, etc.
-
trySetValue
@Deprecated public boolean trySetValue(byte[] newValue) throws java.lang.ExceptionDeprecated.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:
java.lang.Exception- ZK errors, interruptions, etc.
-
trySetValue
public boolean trySetValue(VersionedValue<byte[]> previous, byte[] newValue) throws java.lang.Exception
Changes the shared value only if its value has not changed since the version specified by newValue. 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:
java.lang.Exception- ZK errors, interruptions, etc.
-
getListenable
public org.apache.curator.framework.listen.ListenerContainer<SharedValueListener> getListenable()
Returns the listenable- Specified by:
getListenablein interfaceSharedValueReader- Returns:
- listenable
-
start
public void start() throws java.lang.ExceptionThe shared value must be started before it can be used. Callclose()when you are finished with the shared value- 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
-
-