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 and Description |
---|
DistributedAtomicInteger(CuratorFramework client,
java.lang.String counterPath,
RetryPolicy retryPolicy)
Creates in optimistic mode only - i.e.
|
DistributedAtomicInteger(CuratorFramework client,
java.lang.String counterPath,
RetryPolicy retryPolicy,
PromotedToLock promotedToLock)
Creates in mutex promotion mode.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public DistributedAtomicInteger(CuratorFramework client, java.lang.String counterPath, RetryPolicy retryPolicy)
client
- the clientcounterPath
- path to hold the valueretryPolicy
- the retry policy to usepublic DistributedAtomicInteger(CuratorFramework client, java.lang.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<java.lang.Integer> get() throws java.lang.Exception
DistributedAtomicNumber
0
is returned.get
in interface DistributedAtomicNumber<java.lang.Integer>
java.lang.Exception
- ZooKeeper errorspublic void forceSet(java.lang.Integer newValue) throws java.lang.Exception
DistributedAtomicNumber
forceSet
in interface DistributedAtomicNumber<java.lang.Integer>
newValue
- the new valuejava.lang.Exception
- ZooKeeper errorspublic AtomicValue<java.lang.Integer> compareAndSet(java.lang.Integer expectedValue, java.lang.Integer newValue) throws java.lang.Exception
DistributedAtomicNumber
==
the expected value.
Remember to always check AtomicValue.succeeded()
.compareAndSet
in interface DistributedAtomicNumber<java.lang.Integer>
expectedValue
- the expected valuenewValue
- the new value for the counterjava.lang.Exception
- ZooKeeper errorspublic AtomicValue<java.lang.Integer> trySet(java.lang.Integer newValue) throws java.lang.Exception
DistributedAtomicNumber
AtomicValue.succeeded()
.trySet
in interface DistributedAtomicNumber<java.lang.Integer>
newValue
- the value to setjava.lang.Exception
- ZooKeeper errorspublic AtomicValue<java.lang.Integer> increment() throws java.lang.Exception
AtomicValue.succeeded()
.increment
in interface DistributedAtomicNumber<java.lang.Integer>
java.lang.Exception
- ZooKeeper errorspublic AtomicValue<java.lang.Integer> decrement() throws java.lang.Exception
AtomicValue.succeeded()
.decrement
in interface DistributedAtomicNumber<java.lang.Integer>
java.lang.Exception
- ZooKeeper errorspublic AtomicValue<java.lang.Integer> add(java.lang.Integer delta) throws java.lang.Exception
AtomicValue.succeeded()
.add
in interface DistributedAtomicNumber<java.lang.Integer>
delta
- amount to addjava.lang.Exception
- ZooKeeper errorspublic AtomicValue<java.lang.Integer> subtract(java.lang.Integer delta) throws java.lang.Exception
AtomicValue.succeeded()
.subtract
in interface DistributedAtomicNumber<java.lang.Integer>
delta
- amount to subtractjava.lang.Exception
- ZooKeeper errors