Class DistributedAtomicInteger
java.lang.Object
org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger
- All Implemented Interfaces:
DistributedAtomicNumber<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
ConstructorsConstructorDescriptionDistributedAtomicInteger(CuratorFramework client, String counterPath, RetryPolicy retryPolicy) Creates in optimistic mode only - i.e.DistributedAtomicInteger(CuratorFramework client, String counterPath, RetryPolicy retryPolicy, PromotedToLock promotedToLock) Creates in mutex promotion mode. -
Method Summary
Modifier and TypeMethodDescriptionAdd delta to the current value and return the new value information.compareAndSet(Integer expectedValue, Integer newValue) Atomically sets the value to the given updated value if the current value==the expected value.Subtract 1 from the current value and return the new value information.voidForcibly sets the value of the counter without any guarantees of atomicity.get()Returns the current value of the counter.Add 1 to the current value and return the new value information.booleaninitialize(Integer initialize) Atomic values are initially set to the equivalent ofNULLin a database.Subtract delta from the current value and return the new value information.Attempt to atomically set the value to the given value.
-
Constructor Details
-
DistributedAtomicInteger
public DistributedAtomicInteger(CuratorFramework client, String counterPath, 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
-
DistributedAtomicInteger
public DistributedAtomicInteger(CuratorFramework client, String counterPath, 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 Details
-
get
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<Integer>- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-
forceSet
Description copied from interface:DistributedAtomicNumberForcibly sets the value of the counter without any guarantees of atomicity.- Specified by:
forceSetin interfaceDistributedAtomicNumber<Integer>- Parameters:
newValue- the new value- Throws:
Exception- ZooKeeper errors
-
compareAndSet
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<Integer>- Parameters:
expectedValue- the expected valuenewValue- the new value for the counter- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-
trySet
Description copied from interface:DistributedAtomicNumberAttempt to atomically set the value to the given value. Remember to always checkAtomicValue.succeeded().- Specified by:
trySetin interfaceDistributedAtomicNumber<Integer>- Parameters:
newValue- the value to set- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-
initialize
Description 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<Integer>- Parameters:
initialize- the initial value to set- Returns:
- true if the value was set, false if the node already existed
- Throws:
Exception- ZooKeeper errors
-
increment
Add 1 to the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
incrementin interfaceDistributedAtomicNumber<Integer>- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-
decrement
Subtract 1 from the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
decrementin interfaceDistributedAtomicNumber<Integer>- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-
add
Add delta to the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
addin interfaceDistributedAtomicNumber<Integer>- Parameters:
delta- amount to add- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-
subtract
Subtract delta from the current value and return the new value information. Remember to always checkAtomicValue.succeeded().- Specified by:
subtractin interfaceDistributedAtomicNumber<Integer>- Parameters:
delta- amount to subtract- Returns:
- value info
- Throws:
Exception- ZooKeeper errors
-