Class DistributedAtomicValue
- java.lang.Object
-
- org.apache.curator.framework.recipes.atomic.DistributedAtomicValue
-
public class DistributedAtomicValue extends java.lang.ObjectA distributed value that attempts atomic sets. It first tries uses optimistic locking. If that fails, an optional
InterProcessMutexis taken. For both optimistic and mutex, a retry policy is used to retry the increment.The various methods return an
AtomicValueobject. You must always checkAtomicValue.succeeded(). None of the methods (other than get()) are guaranteed to succeed.
-
-
Constructor Summary
Constructors Constructor Description DistributedAtomicValue(org.apache.curator.framework.CuratorFramework client, java.lang.String path, org.apache.curator.RetryPolicy retryPolicy)Creates in optimistic mode only - i.e.DistributedAtomicValue(org.apache.curator.framework.CuratorFramework client, java.lang.String path, org.apache.curator.RetryPolicy retryPolicy, PromotedToLock promotedToLock)Creates in mutex promotion mode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AtomicValue<byte[]>compareAndSet(byte[] expectedValue, byte[] newValue)Atomically sets the value to the given updated value if the current value==the expected value.voidforceSet(byte[] newValue)Forcibly sets the value any guarantees of atomicity.AtomicValue<byte[]>get()Returns the current value of the counter.booleaninitialize(byte[] value)Atomic values are initially set to the equivalent ofNULLin a database.AtomicValue<byte[]>trySet(byte[] newValue)Attempt to atomically set the value to the given value.
-
-
-
Constructor Detail
-
DistributedAtomicValue
public DistributedAtomicValue(org.apache.curator.framework.CuratorFramework client, java.lang.String path, org.apache.curator.RetryPolicy retryPolicy)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(org.apache.curator.framework.CuratorFramework client, java.lang.String path, org.apache.curator.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, aInterProcessMutexwill 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 Detail
-
get
public AtomicValue<byte[]> get() throws java.lang.Exception
Returns the current value of the counter. NOTE: if the value has never been set,0is returned.- Returns:
- value info
- Throws:
java.lang.Exception- ZooKeeper errors
-
forceSet
public void forceSet(byte[] newValue) throws java.lang.ExceptionForcibly sets the value any guarantees of atomicity.- Parameters:
newValue- the new value- Throws:
java.lang.Exception- ZooKeeper errors
-
compareAndSet
public AtomicValue<byte[]> compareAndSet(byte[] expectedValue, byte[] newValue) throws java.lang.Exception
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:
java.lang.Exception- ZooKeeper errors
-
trySet
public AtomicValue<byte[]> trySet(byte[] newValue) throws java.lang.Exception
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:
java.lang.Exception- ZooKeeper errors
-
initialize
public boolean initialize(byte[] value) throws java.lang.ExceptionAtomic values are initially set to the equivalent ofNULLin 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:
java.lang.Exception- ZooKeeper errors
-
-