com.netflix.curator.framework.recipes.shared
Class SharedValue

java.lang.Object
  extended by com.netflix.curator.framework.recipes.shared.SharedValue
All Implemented Interfaces:
SharedValueReader, java.io.Closeable

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
SharedValue(com.netflix.curator.framework.CuratorFramework client, java.lang.String path, byte[] seedValue)
           
 
Method Summary
 void close()
           
 com.netflix.curator.framework.listen.ListenerContainer<SharedValueListener> getListenable()
          Returns the listenable
 byte[] getValue()
          Return the current value of the count
 void setValue(byte[] newValue)
          Change the shared value value irrespective of its previous state
 void start()
          The shared value must be started before it can be used.
 boolean trySetValue(byte[] newValue)
          Changes the shared value only if its value has not changed since this client last read it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SharedValue

public SharedValue(com.netflix.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
Method Detail

getValue

public byte[] getValue()
Description copied from interface: SharedValueReader
Return the current value of the count

Specified by:
getValue in interface SharedValueReader
Returns:
count

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

public boolean trySetValue(byte[] newValue)
                    throws java.lang.Exception
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.

getListenable

public com.netflix.curator.framework.listen.ListenerContainer<SharedValueListener> getListenable()
Returns the listenable

Specified by:
getListenable in interface SharedValueReader
Returns:
listenable

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.io.Closeable
Throws:
java.io.IOException