Class DistributedAtomicValue

java.lang.Object
org.apache.curator.framework.recipes.atomic.DistributedAtomicValue

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 Details

    • DistributedAtomicValue

      public DistributedAtomicValue(CuratorFramework client, String path, RetryPolicy retryPolicy)
      Creates in optimistic mode only - i.e. the promotion to a mutex is not done
      Parameters:
      client - the client
      path - path to hold the value
      retryPolicy - 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, a InterProcessMutex will be tried with its own retry policy
      Parameters:
      client - the client
      path - path to hold the value
      retryPolicy - the retry policy to use
      promotedToLock - the arguments for the mutex promotion
  • Method Details

    • get

      public AtomicValue<byte[]> get() throws Exception
      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

      public void forceSet(byte[] newValue) throws Exception
      Forcibly sets the value any guarantees of atomicity.
      Parameters:
      newValue - the new value
      Throws:
      Exception - ZooKeeper errors
    • compareAndSet

      public AtomicValue<byte[]> compareAndSet(byte[] expectedValue, byte[] newValue) throws Exception
      Atomically sets the value to the given updated value if the current value == the expected value. Remember to always check AtomicValue.succeeded().
      Parameters:
      expectedValue - the expected value
      newValue - the new value
      Returns:
      value info
      Throws:
      Exception - ZooKeeper errors
    • trySet

      public AtomicValue<byte[]> trySet(byte[] newValue) throws Exception
      Attempt to atomically set the value to the given value. Remember to always check AtomicValue.succeeded().
      Parameters:
      newValue - the value to set
      Returns:
      value info
      Throws:
      Exception - ZooKeeper errors
    • initialize

      public boolean initialize(byte[] value) throws Exception
      Atomic values are initially set to the equivalent of 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.
      Parameters:
      value - the initial value to set
      Returns:
      true if the value was set, false if the node already existed
      Throws:
      Exception - ZooKeeper errors