Class DistributedAtomicLong
- java.lang.Object
-
- org.apache.curator.framework.recipes.atomic.DistributedAtomicLong
-
- All Implemented Interfaces:
DistributedAtomicNumber<java.lang.Long>
public class DistributedAtomicLong extends java.lang.Object implements DistributedAtomicNumber<java.lang.Long>
A counter that attempts atomic increments. It first tries uses optimistic locking. If that fails, an optional
InterProcessMutexis taken. For both optimistic and mutex, a retry policy is used to retry the increment.The various increment methods return an
AtomicValueobject. You must always checkAtomicValue.succeeded(). None of the methods (other than get()) are guaranteed to succeed.
-
-
Constructor Summary
Constructors Constructor Description DistributedAtomicLong(org.apache.curator.framework.CuratorFramework client, java.lang.String counterPath, org.apache.curator.RetryPolicy retryPolicy)Creates in optimistic mode only - i.e.DistributedAtomicLong(org.apache.curator.framework.CuratorFramework client, java.lang.String counterPath, org.apache.curator.RetryPolicy retryPolicy, PromotedToLock promotedToLock)Creates in mutex promotion mode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AtomicValue<java.lang.Long>add(java.lang.Long delta)Add delta to the current value and return the new value information.AtomicValue<java.lang.Long>compareAndSet(java.lang.Long expectedValue, java.lang.Long newValue)Atomically sets the value to the given updated value if the current value==the expected value.AtomicValue<java.lang.Long>decrement()Subtract 1 from the current value and return the new value information.voidforceSet(java.lang.Long newValue)Forcibly sets the value of the counter without any guarantees of atomicity.AtomicValue<java.lang.Long>get()Returns the current value of the counter.AtomicValue<java.lang.Long>increment()Add 1 to the current value and return the new value information.booleaninitialize(java.lang.Long initialize)Atomic values are initially set to the equivalent ofNULLin a database.AtomicValue<java.lang.Long>subtract(java.lang.Long delta)Subtract delta from the current value and return the new value information.AtomicValue<java.lang.Long>trySet(java.lang.Long newValue)Attempt to atomically set the value to the given value.
-
-
-
Constructor Detail
-
DistributedAtomicLong
public DistributedAtomicLong(org.apache.curator.framework.CuratorFramework client, java.lang.String counterPath, org.apache.curator.RetryPolicy retryPolicy)Creates in optimistic mode only - i.e. the promotion to a mutex is not done- Parameters:
client- the clientcounterPath- path to hold the valueretryPolicy- the retry policy to use
-
DistributedAtomicLong
public DistributedAtomicLong(org.apache.curator.framework.CuratorFramework client, java.lang.String counterPath, org.apache.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, aInterProcessMutexwill be tried with its own retry policy- Parameters:
client- the clientcounterPath- path to hold the valueretryPolicy- the retry policy to usepromotedToLock- the arguments for the mutex promotion
-
-
Method Detail
-
get
public AtomicValue<java.lang.Long> get() throws java.lang.Exception
Description copied from interface:DistributedAtomicNumberReturns the current value of the counter. NOTE: if the value has never been set,0is returned.- Specified by:
getin interfaceDistributedAtomicNumber<java.lang.Long>- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
forceSet
public void forceSet(java.lang.Long newValue) throws java.lang.ExceptionDescription copied from interface:DistributedAtomicNumberForcibly sets the value of the counter without any guarantees of atomicity.- Specified by:
forceSetin interfaceDistributedAtomicNumber<java.lang.Long>- Parameters:
newValue- the new value- Throws:
java.lang.Exception- ZooKeeper errors
-
compareAndSet
public AtomicValue<java.lang.Long> compareAndSet(java.lang.Long expectedValue, java.lang.Long newValue) throws java.lang.Exception
Description copied from interface:DistributedAtomicNumberAtomically sets the value to the given updated value if the current value==the expected value. Remember to always checkAtomicValue.succeeded().- Specified by:
compareAndSetin interfaceDistributedAtomicNumber<java.lang.Long>- Parameters:
expectedValue- the expected valuenewValue- the new value for the counter- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
trySet
public AtomicValue<java.lang.Long> trySet(java.lang.Long newValue) throws java.lang.Exception
Description copied from interface:DistributedAtomicNumberAttempt to atomically set the value to the given value. Remember to always checkAtomicValue.succeeded().- Specified by:
trySetin interfaceDistributedAtomicNumber<java.lang.Long>- Parameters:
newValue- the value to set- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
initialize
public boolean initialize(java.lang.Long initialize) throws java.lang.ExceptionDescription copied from interface:DistributedAtomicNumberAtomic values are initially set to the equivalent ofNULLin a database. Use this method to initialize the value. The value will be set if and only iff the node does not exist.- Specified by:
initializein interfaceDistributedAtomicNumber<java.lang.Long>- Parameters:
initialize- the initial value to set- Returns:
- true if the value was set, false if the node already existed
- Throws:
java.lang.Exception- ZooKeeper errors
-
increment
public AtomicValue<java.lang.Long> increment() throws java.lang.Exception
Add 1 to the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
incrementin interfaceDistributedAtomicNumber<java.lang.Long>- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
decrement
public AtomicValue<java.lang.Long> decrement() throws java.lang.Exception
Subtract 1 from the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
decrementin interfaceDistributedAtomicNumber<java.lang.Long>- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
add
public AtomicValue<java.lang.Long> add(java.lang.Long delta) throws java.lang.Exception
Add delta to the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
addin interfaceDistributedAtomicNumber<java.lang.Long>- Parameters:
delta- amount to add- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
subtract
public AtomicValue<java.lang.Long> subtract(java.lang.Long delta) throws java.lang.Exception
Subtract delta from the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
subtractin interfaceDistributedAtomicNumber<java.lang.Long>- Parameters:
delta- amount to subtract- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
-