org.apache.curator.framework.recipes.shared
Class SharedValue

java.lang.Object
  extended by org.apache.curator.framework.recipes.shared.SharedValue
All Implemented Interfaces:
Closeable, 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 Summary
SharedValue(org.apache.curator.framework.CuratorFramework client, String path, byte[] seedValue)
           
 
Method Summary
 void close()
           
 org.apache.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(org.apache.curator.framework.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
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 Exception
Change the shared value value irrespective of its previous state

Parameters:
newValue - new value
Throws:
Exception - ZK errors, interruptions, etc.

trySetValue

public boolean trySetValue(byte[] newValue)
                    throws 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:
Exception - ZK errors, interruptions, etc.

getListenable

public org.apache.curator.framework.listen.ListenerContainer<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 Closeable
Throws:
IOException


Copyright © 2011–2014 The Apache Software Foundation. All rights reserved.