public class DistributedAtomicInteger extends Object implements 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 and Description |
|---|
DistributedAtomicInteger(org.apache.curator.framework.CuratorFramework client,
String counterPath,
org.apache.curator.RetryPolicy retryPolicy)
Creates in optimistic mode only - i.e.
|
DistributedAtomicInteger(org.apache.curator.framework.CuratorFramework client,
String counterPath,
org.apache.curator.RetryPolicy retryPolicy,
PromotedToLock promotedToLock)
Creates in mutex promotion mode.
|
| Modifier and Type | Method and Description |
|---|---|
AtomicValue<Integer> |
add(Integer delta)
Add delta to the current value and return the new value information.
|
AtomicValue<Integer> |
compareAndSet(Integer expectedValue,
Integer newValue)
Atomically sets the value to the given updated value
if the current value
== the expected value. |
AtomicValue<Integer> |
decrement()
Subtract 1 from the current value and return the new value information.
|
void |
forceSet(Integer newValue)
Forcibly sets the value of the counter without any guarantees of atomicity.
|
AtomicValue<Integer> |
get()
Returns the current value of the counter.
|
AtomicValue<Integer> |
increment()
Add 1 to the current value and return the new value information.
|
boolean |
initialize(Integer initialize)
Atomic values are initially set to the equivalent of
NULL in a database. |
AtomicValue<Integer> |
subtract(Integer delta)
Subtract delta from the current value and return the new value information.
|
AtomicValue<Integer> |
trySet(Integer newValue)
Attempt to atomically set the value to the given value.
|
public DistributedAtomicInteger(org.apache.curator.framework.CuratorFramework client,
String counterPath,
org.apache.curator.RetryPolicy retryPolicy)
client - the clientcounterPath - path to hold the valueretryPolicy - the retry policy to usepublic DistributedAtomicInteger(org.apache.curator.framework.CuratorFramework client,
String counterPath,
org.apache.curator.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<Integer> get() throws Exception
DistributedAtomicNumber0 is returned.get in interface DistributedAtomicNumber<Integer>Exception - ZooKeeper errorspublic void forceSet(Integer newValue) throws Exception
DistributedAtomicNumberforceSet in interface DistributedAtomicNumber<Integer>newValue - the new valueException - ZooKeeper errorspublic AtomicValue<Integer> compareAndSet(Integer expectedValue, Integer newValue) throws Exception
DistributedAtomicNumber== the expected value.
Remember to always check AtomicValue.succeeded().compareAndSet in interface DistributedAtomicNumber<Integer>expectedValue - the expected valuenewValue - the new value for the counterException - ZooKeeper errorspublic AtomicValue<Integer> trySet(Integer newValue) throws Exception
DistributedAtomicNumberAtomicValue.succeeded().trySet in interface DistributedAtomicNumber<Integer>newValue - the value to setException - ZooKeeper errorspublic boolean initialize(Integer initialize) throws Exception
DistributedAtomicNumberNULL 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<Integer>initialize - the initial value to setException - ZooKeeper errorspublic AtomicValue<Integer> increment() throws Exception
AtomicValue.succeeded().increment in interface DistributedAtomicNumber<Integer>Exception - ZooKeeper errorspublic AtomicValue<Integer> decrement() throws Exception
AtomicValue.succeeded().decrement in interface DistributedAtomicNumber<Integer>Exception - ZooKeeper errorspublic AtomicValue<Integer> add(Integer delta) throws Exception
AtomicValue.succeeded().add in interface DistributedAtomicNumber<Integer>delta - amount to addException - ZooKeeper errorspublic AtomicValue<Integer> subtract(Integer delta) throws Exception
AtomicValue.succeeded().subtract in interface DistributedAtomicNumber<Integer>delta - amount to subtractException - ZooKeeper errorsCopyright © 2011–2018 The Apache Software Foundation. All rights reserved.