com.netflix.curator.framework.recipes.atomic
Class DistributedAtomicValue

java.lang.Object
  extended by com.netflix.curator.framework.recipes.atomic.DistributedAtomicValue

public class DistributedAtomicValue
extends java.lang.Object

A distributed value that attempts atomic sets. It first tries uses optimistic locking. If that fails, an optional InterProcessMutex is taken. For both optimistic and mutex, a retry policy is used to retry the increment.

The various methods return an AtomicValue object. You must always check AtomicValue.succeeded(). None of the methods (other than get()) are guaranteed to succeed.


Constructor Summary
DistributedAtomicValue(com.netflix.curator.framework.CuratorFramework client, java.lang.String path, com.netflix.curator.RetryPolicy retryPolicy)
          Creates in optimistic mode only - i.e.
DistributedAtomicValue(com.netflix.curator.framework.CuratorFramework client, java.lang.String path, com.netflix.curator.RetryPolicy retryPolicy, PromotedToLock promotedToLock)
          Creates in mutex promotion mode.
 
Method Summary
 AtomicValue<byte[]> compareAndSet(byte[] expectedValue, byte[] newValue)
          Atomically sets the value to the given updated value if the current value == the expected value.
 void forceSet(byte[] newValue)
          Forcibly sets the value any guarantees of atomicity.
 AtomicValue<byte[]> get()
          Returns the current value of the counter.
 AtomicValue<byte[]> trySet(byte[] newValue)
          Attempt to atomically set the value to the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DistributedAtomicValue

public DistributedAtomicValue(com.netflix.curator.framework.CuratorFramework client,
                              java.lang.String path,
                              com.netflix.curator.RetryPolicy retryPolicy)
Creates in optimistic mode only - i.e. the promotion to a mutex is not done

Parameters:
client - the client
path - path to hold the value
retryPolicy - the retry policy to use

DistributedAtomicValue

public DistributedAtomicValue(com.netflix.curator.framework.CuratorFramework client,
                              java.lang.String path,
                              com.netflix.curator.RetryPolicy retryPolicy,
                              PromotedToLock promotedToLock)
Creates in mutex promotion mode. The optimistic lock will be tried first using the given retry policy. If the increment does not succeed, a InterProcessMutex will be tried with its own retry policy

Parameters:
client - the client
path - path to hold the value
retryPolicy - the retry policy to use
promotedToLock - the arguments for the mutex promotion
Method Detail

get

public AtomicValue<byte[]> get()
                        throws java.lang.Exception
Returns the current value of the counter. NOTE: if the value has never been set, 0 is returned.

Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

forceSet

public void forceSet(byte[] newValue)
              throws java.lang.Exception
Forcibly sets the value any guarantees of atomicity.

Parameters:
newValue - the new value
Throws:
java.lang.Exception - ZooKeeper errors

compareAndSet

public AtomicValue<byte[]> compareAndSet(byte[] expectedValue,
                                         byte[] newValue)
                                  throws java.lang.Exception
Atomically sets the value to the given updated value if the current value == the expected value. Remember to always check AtomicValue.succeeded().

Parameters:
expectedValue - the expected value
newValue - the new value
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

trySet

public AtomicValue<byte[]> trySet(byte[] newValue)
                           throws java.lang.Exception
Attempt to atomically set the value to the given value. Remember to always check AtomicValue.succeeded().

Parameters:
newValue - the value to set
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors