Class SharedValue

java.lang.Object
org.apache.curator.framework.recipes.shared.SharedValue
All Implemented Interfaces:
Closeable, AutoCloseable, SharedValueReader

public class SharedValue extends Object implements 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 Details

    • SharedValue

      public SharedValue(CuratorFramework client, String path, byte[] seedValue)
      Parameters:
      client - the client
      path - the shared path - i.e. where the shared value is stored
      seedValue - 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 interface SharedValueReader
      Returns:
      count
    • getVersionedValue

      public VersionedValue<byte[]> getVersionedValue()
      Description copied from interface: SharedValueReader
      Return the current version and value
      Specified by:
      getVersionedValue in interface SharedValueReader
      Returns:
      version/value
    • setValue

      public void setValue(byte[] newValue) throws Exception
      Change the shared value irrespective of its previous state
      Parameters:
      newValue - new value
      Throws:
      Exception - ZK errors, interruptions, etc.
    • trySetValue

      @Deprecated public boolean trySetValue(byte[] newValue) throws Exception
      Deprecated.
      use trySetValue(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 calling getValue().
      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.
    • trySetValue

      public boolean trySetValue(VersionedValue<byte[]> previous, byte[] newValue) throws 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 calling getValue().
      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:
      IllegalTrySetVersionException - if Stat.getVersion() overflowed to -1
      Exception - ZK errors, interruptions, etc.
    • getListenable

      public Listenable<SharedValueListener> getListenable()
      Returns the listenable
      Specified by:
      getListenable in interface SharedValueReader
      Returns:
      listenable
    • start

      public void start() throws Exception
      The shared value must be started before it can be used. Call close() when you are finished with the shared value
      Throws:
      Exception - ZK errors, interruptions, etc.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException