public class DistributedAtomicLong extends Object implements 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 and Description |
---|
DistributedAtomicLong(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.
|
Modifier and Type | Method and Description |
---|---|
AtomicValue<Long> |
add(Long delta)
Add delta to the current value and return the new value information.
|
AtomicValue<Long> |
compareAndSet(Long expectedValue,
Long newValue)
Atomically sets the value to the given updated value
if the current value
== the expected value. |
AtomicValue<Long> |
decrement()
Subtract 1 from the current value and return the new value information.
|
void |
forceSet(Long newValue)
Forcibly sets the value of the counter without any guarantees of atomicity.
|
AtomicValue<Long> |
get()
Returns the current value of the counter.
|
AtomicValue<Long> |
increment()
Add 1 to the current value and return the new value information.
|
boolean |
initialize(Long initialize)
Atomic values are initially set to the equivalent of
NULL in a database. |
AtomicValue<Long> |
subtract(Long delta)
Subtract delta from the current value and return the new value information.
|
AtomicValue<Long> |
trySet(Long newValue)
Attempt to atomically set the value to the given value.
|
public DistributedAtomicLong(CuratorFramework client, String counterPath, RetryPolicy retryPolicy)
client
- the clientcounterPath
- path to hold the valueretryPolicy
- the retry policy to usepublic DistributedAtomicLong(CuratorFramework client, String counterPath, RetryPolicy retryPolicy, PromotedToLock promotedToLock)
InterProcessMutex
will be tried
with its own retry policyclient
- the clientcounterPath
- path to hold the valueretryPolicy
- the retry policy to usepromotedToLock
- the arguments for the mutex promotionpublic AtomicValue<Long> get() throws Exception
DistributedAtomicNumber
0
is returned.get
in interface DistributedAtomicNumber<Long>
Exception
- ZooKeeper errorspublic void forceSet(Long newValue) throws Exception
DistributedAtomicNumber
forceSet
in interface DistributedAtomicNumber<Long>
newValue
- the new valueException
- ZooKeeper errorspublic AtomicValue<Long> compareAndSet(Long expectedValue, Long newValue) throws Exception
DistributedAtomicNumber
==
the expected value.
Remember to always check AtomicValue.succeeded()
.compareAndSet
in interface DistributedAtomicNumber<Long>
expectedValue
- the expected valuenewValue
- the new value for the counterException
- ZooKeeper errorspublic AtomicValue<Long> trySet(Long newValue) throws Exception
DistributedAtomicNumber
AtomicValue.succeeded()
.trySet
in interface DistributedAtomicNumber<Long>
newValue
- the value to setException
- ZooKeeper errorspublic boolean initialize(Long initialize) throws Exception
DistributedAtomicNumber
NULL
in a database.
Use this method to initialize the value. The value will be set if and only iff the node does not exist.initialize
in interface DistributedAtomicNumber<Long>
initialize
- the initial value to setException
- ZooKeeper errorspublic AtomicValue<Long> increment() throws Exception
AtomicValue.succeeded()
.increment
in interface DistributedAtomicNumber<Long>
Exception
- ZooKeeper errorspublic AtomicValue<Long> decrement() throws Exception
AtomicValue.succeeded()
.decrement
in interface DistributedAtomicNumber<Long>
Exception
- ZooKeeper errorspublic AtomicValue<Long> add(Long delta) throws Exception
AtomicValue.succeeded()
.add
in interface DistributedAtomicNumber<Long>
delta
- amount to addException
- ZooKeeper errorspublic AtomicValue<Long> subtract(Long delta) throws Exception
AtomicValue.succeeded()
.subtract
in interface DistributedAtomicNumber<Long>
delta
- amount to subtractException
- ZooKeeper errorsCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.