Class DistributedAtomicLong
java.lang.Object
org.apache.curator.framework.recipes.atomic.DistributedAtomicLong
- All Implemented Interfaces:
DistributedAtomicNumber<Long>
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
ConstructorDescriptionDistributedAtomicLong
(CuratorFramework client, String counterPath, RetryPolicy retryPolicy) Creates in optimistic mode only - i.e.DistributedAtomicLong
(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
(Long expectedValue, Long 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.void
Forcibly 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.boolean
initialize
(Long initialize) Atomic values are initially set to the equivalent ofNULL
in 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
-
DistributedAtomicLong
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(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, aInterProcessMutex
will 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:DistributedAtomicNumber
Returns the current value of the counter. NOTE: if the value has never been set,0
is returned.- Specified by:
get
in interfaceDistributedAtomicNumber<Long>
- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-
forceSet
Description copied from interface:DistributedAtomicNumber
Forcibly sets the value of the counter without any guarantees of atomicity.- Specified by:
forceSet
in interfaceDistributedAtomicNumber<Long>
- Parameters:
newValue
- the new value- Throws:
Exception
- ZooKeeper errors
-
compareAndSet
Description copied from interface:DistributedAtomicNumber
Atomically sets the value to the given updated value if the current value==
the expected value. Remember to always checkAtomicValue.succeeded()
.- Specified by:
compareAndSet
in interfaceDistributedAtomicNumber<Long>
- Parameters:
expectedValue
- the expected valuenewValue
- the new value for the counter- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-
trySet
Description copied from interface:DistributedAtomicNumber
Attempt to atomically set the value to the given value. Remember to always checkAtomicValue.succeeded()
.- Specified by:
trySet
in interfaceDistributedAtomicNumber<Long>
- Parameters:
newValue
- the value to set- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-
initialize
Description copied from interface:DistributedAtomicNumber
Atomic values are initially set to the equivalent ofNULL
in 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:
initialize
in interfaceDistributedAtomicNumber<Long>
- 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:
increment
in interfaceDistributedAtomicNumber<Long>
- 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:
decrement
in interfaceDistributedAtomicNumber<Long>
- 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:
add
in interfaceDistributedAtomicNumber<Long>
- 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:
subtract
in interfaceDistributedAtomicNumber<Long>
- Parameters:
delta
- amount to subtract- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-