Class DistributedAtomicValue
java.lang.Object
org.apache.curator.framework.recipes.atomic.DistributedAtomicValue
A distributed value that attempts atomic sets. 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 methods return an AtomicValue
object. You must always check
AtomicValue.succeeded()
. None of the methods (other than get()) are guaranteed to succeed.
-
Constructor Summary
ConstructorDescriptionDistributedAtomicValue
(CuratorFramework client, String path, RetryPolicy retryPolicy) Creates in optimistic mode only - i.e.DistributedAtomicValue
(CuratorFramework client, String path, RetryPolicy retryPolicy, PromotedToLock promotedToLock) Creates in mutex promotion mode. -
Method Summary
Modifier and TypeMethodDescriptionAtomicValue
<byte[]> compareAndSet
(byte[] expectedValue, byte[] newValue) Atomically sets the value to the given updated value if the current value==
the expected value.void
forceSet
(byte[] newValue) Forcibly sets the value any guarantees of atomicity.AtomicValue
<byte[]> get()
Returns the current value of the counter.boolean
initialize
(byte[] value) Atomic values are initially set to the equivalent ofNULL
in a database.AtomicValue
<byte[]> trySet
(byte[] newValue) Attempt to atomically set the value to the given value.
-
Constructor Details
-
DistributedAtomicValue
Creates in optimistic mode only - i.e. the promotion to a mutex is not done- Parameters:
client
- the clientpath
- path to hold the valueretryPolicy
- the retry policy to use
-
DistributedAtomicValue
public DistributedAtomicValue(CuratorFramework client, String path, 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 clientpath
- path to hold the valueretryPolicy
- the retry policy to usepromotedToLock
- the arguments for the mutex promotion
-
-
Method Details
-
get
Returns the current value of the counter. NOTE: if the value has never been set,0
is returned.- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-
forceSet
Forcibly sets the value any guarantees of atomicity.- Parameters:
newValue
- the new value- Throws:
Exception
- ZooKeeper errors
-
compareAndSet
Atomically sets the value to the given updated value if the current value==
the expected value. Remember to always checkAtomicValue.succeeded()
.- Parameters:
expectedValue
- the expected valuenewValue
- the new value- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-
trySet
Attempt to atomically set the value to the given value. Remember to always checkAtomicValue.succeeded()
.- Parameters:
newValue
- the value to set- Returns:
- value info
- Throws:
Exception
- ZooKeeper errors
-
initialize
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.- Parameters:
value
- the initial value to set- Returns:
- true if the value was set, false if the node already existed
- Throws:
Exception
- ZooKeeper errors
-