public class DistributedAtomicValue extends Object
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 and Description |
---|
DistributedAtomicValue(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.
|
Modifier and Type | Method and Description |
---|---|
AtomicValue<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 of
NULL in a database. |
AtomicValue<byte[]> |
trySet(byte[] newValue)
Attempt to atomically set the value to the given value.
|
public DistributedAtomicValue(CuratorFramework client, String path, RetryPolicy retryPolicy)
client
- the clientpath
- path to hold the valueretryPolicy
- the retry policy to usepublic DistributedAtomicValue(CuratorFramework client, String path, RetryPolicy retryPolicy, PromotedToLock promotedToLock)
InterProcessMutex
will be tried
with its own retry policyclient
- the clientpath
- path to hold the valueretryPolicy
- the retry policy to usepromotedToLock
- the arguments for the mutex promotionpublic AtomicValue<byte[]> get() throws Exception
0
is returned.Exception
- ZooKeeper errorspublic void forceSet(byte[] newValue) throws Exception
newValue
- the new valueException
- ZooKeeper errorspublic AtomicValue<byte[]> compareAndSet(byte[] expectedValue, byte[] newValue) throws Exception
==
the expected value.
Remember to always check AtomicValue.succeeded()
.expectedValue
- the expected valuenewValue
- the new valueException
- ZooKeeper errorspublic AtomicValue<byte[]> trySet(byte[] newValue) throws Exception
AtomicValue.succeeded()
.newValue
- the value to setException
- ZooKeeper errorspublic boolean initialize(byte[] value) throws Exception
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.value
- the initial value to setException
- ZooKeeper errorsCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.