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

java.lang.Object
  extended by com.netflix.curator.framework.recipes.atomic.DistributedAtomicInteger
All Implemented Interfaces:
DistributedAtomicNumber<java.lang.Integer>

public class DistributedAtomicInteger
extends java.lang.Object
implements DistributedAtomicNumber<java.lang.Integer>

A counter that attempts atomic increments. 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 increment methods return an AtomicValue object. You must always check AtomicValue.succeeded(). None of the methods (other than get()) are guaranteed to succeed.


Constructor Summary
DistributedAtomicInteger(com.netflix.curator.framework.CuratorFramework client, java.lang.String counterPath, com.netflix.curator.RetryPolicy retryPolicy)
          Creates in optimistic mode only - i.e.
DistributedAtomicInteger(com.netflix.curator.framework.CuratorFramework client, java.lang.String counterPath, com.netflix.curator.RetryPolicy retryPolicy, PromotedToLock promotedToLock)
          Creates in mutex promotion mode.
 
Method Summary
 AtomicValue<java.lang.Integer> add(java.lang.Integer delta)
          Add delta to the current value and return the new value information.
 AtomicValue<java.lang.Integer> compareAndSet(java.lang.Integer expectedValue, java.lang.Integer newValue)
          Atomically sets the value to the given updated value if the current value == the expected value.
 AtomicValue<java.lang.Integer> decrement()
          Subtract 1 from the current value and return the new value information.
 void forceSet(java.lang.Integer newValue)
          Forcibly sets the value of the counter without any guarantees of atomicity.
 AtomicValue<java.lang.Integer> get()
          Returns the current value of the counter.
 AtomicValue<java.lang.Integer> increment()
          Add 1 to the current value and return the new value information.
 AtomicValue<java.lang.Integer> subtract(java.lang.Integer delta)
          Subtract delta from the current value and return the new value information.
 AtomicValue<java.lang.Integer> trySet(java.lang.Integer 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

DistributedAtomicInteger

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

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

DistributedAtomicInteger

public DistributedAtomicInteger(com.netflix.curator.framework.CuratorFramework client,
                                java.lang.String counterPath,
                                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
counterPath - path to hold the value
retryPolicy - the retry policy to use
promotedToLock - the arguments for the mutex promotion
Method Detail

get

public AtomicValue<java.lang.Integer> get()
                                   throws java.lang.Exception
Description copied from interface: DistributedAtomicNumber
Returns the current value of the counter. NOTE: if the value has never been set, 0 is returned.

Specified by:
get in interface DistributedAtomicNumber<java.lang.Integer>
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

forceSet

public void forceSet(java.lang.Integer newValue)
              throws java.lang.Exception
Description copied from interface: DistributedAtomicNumber
Forcibly sets the value of the counter without any guarantees of atomicity.

Specified by:
forceSet in interface DistributedAtomicNumber<java.lang.Integer>
Parameters:
newValue - the new value
Throws:
java.lang.Exception - ZooKeeper errors

compareAndSet

public AtomicValue<java.lang.Integer> compareAndSet(java.lang.Integer expectedValue,
                                                    java.lang.Integer newValue)
                                             throws java.lang.Exception
Description copied from interface: DistributedAtomicNumber
Atomically sets the value to the given updated value if the current value == the expected value. Remember to always check AtomicValue.succeeded().

Specified by:
compareAndSet in interface DistributedAtomicNumber<java.lang.Integer>
Parameters:
expectedValue - the expected value
newValue - the new value for the counter
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

trySet

public AtomicValue<java.lang.Integer> trySet(java.lang.Integer newValue)
                                      throws java.lang.Exception
Description copied from interface: DistributedAtomicNumber
Attempt to atomically set the value to the given value. Remember to always check AtomicValue.succeeded().

Specified by:
trySet in interface DistributedAtomicNumber<java.lang.Integer>
Parameters:
newValue - the value to set
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

increment

public AtomicValue<java.lang.Integer> increment()
                                         throws java.lang.Exception
Add 1 to the current value and return the new value information. Remember to always check AtomicValue.succeeded().

Specified by:
increment in interface DistributedAtomicNumber<java.lang.Integer>
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

decrement

public AtomicValue<java.lang.Integer> decrement()
                                         throws java.lang.Exception
Subtract 1 from the current value and return the new value information. Remember to always check AtomicValue.succeeded().

Specified by:
decrement in interface DistributedAtomicNumber<java.lang.Integer>
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

add

public AtomicValue<java.lang.Integer> add(java.lang.Integer delta)
                                   throws java.lang.Exception
Add delta to the current value and return the new value information. Remember to always check AtomicValue.succeeded().

Specified by:
add in interface DistributedAtomicNumber<java.lang.Integer>
Parameters:
delta - amount to add
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors

subtract

public AtomicValue<java.lang.Integer> subtract(java.lang.Integer delta)
                                        throws java.lang.Exception
Subtract delta from the current value and return the new value information. Remember to always check AtomicValue.succeeded().

Specified by:
subtract in interface DistributedAtomicNumber<java.lang.Integer>
Parameters:
delta - amount to subtract
Returns:
value info
Throws:
java.lang.Exception - ZooKeeper errors