Class 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)  
      protected SharedValue​(org.apache.curator.framework.WatcherRemoveCuratorFramework client, java.lang.String path, byte[] seedValue, org.apache.curator.framework.api.CuratorWatcher watcher)  
    • Constructor Detail

      • SharedValue

        public SharedValue​(org.apache.curator.framework.CuratorFramework client,
                           java.lang.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​(org.apache.curator.framework.WatcherRemoveCuratorFramework client,
                              java.lang.String path,
                              byte[] seedValue,
                              org.apache.curator.framework.api.CuratorWatcher watcher)
    • Method Detail

      • setValue

        public void setValue​(byte[] newValue)
                      throws java.lang.Exception
        Change 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.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:
        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 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:
        java.lang.Exception - ZK errors, interruptions, etc.
      • start

        public void start()
                   throws java.lang.Exception
        The shared value must be started before it can be used. Call close() 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:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException