Class DistributedAtomicLong

  • All Implemented Interfaces:
    DistributedAtomicNumber<java.lang.Long>

    public class DistributedAtomicLong
    extends java.lang.Object
    implements DistributedAtomicNumber<java.lang.Long>

    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 Summary

      Constructors 
      Constructor Description
      DistributedAtomicLong​(org.apache.curator.framework.CuratorFramework client, java.lang.String counterPath, org.apache.curator.RetryPolicy retryPolicy)
      Creates in optimistic mode only - i.e.
      DistributedAtomicLong​(org.apache.curator.framework.CuratorFramework client, java.lang.String counterPath, 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<java.lang.Long> add​(java.lang.Long delta)
      Add delta to the current value and return the new value information.
      AtomicValue<java.lang.Long> compareAndSet​(java.lang.Long expectedValue, java.lang.Long newValue)
      Atomically sets the value to the given updated value if the current value == the expected value.
      AtomicValue<java.lang.Long> decrement()
      Subtract 1 from the current value and return the new value information.
      void forceSet​(java.lang.Long newValue)
      Forcibly sets the value of the counter without any guarantees of atomicity.
      AtomicValue<java.lang.Long> get()
      Returns the current value of the counter.
      AtomicValue<java.lang.Long> increment()
      Add 1 to the current value and return the new value information.
      boolean initialize​(java.lang.Long initialize)
      Atomic values are initially set to the equivalent of NULL in a database.
      AtomicValue<java.lang.Long> subtract​(java.lang.Long delta)
      Subtract delta from the current value and return the new value information.
      AtomicValue<java.lang.Long> trySet​(java.lang.Long newValue)
      Attempt to atomically set the value to the given value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DistributedAtomicLong

        public DistributedAtomicLong​(org.apache.curator.framework.CuratorFramework client,
                                     java.lang.String counterPath,
                                     org.apache.curator.RetryPolicy retryPolicy)
        Creates in optimistic mode only - i.e. the promotion to a mutex is not done
        Parameters:
        client - the client
        counterPath - path to hold the value
        retryPolicy - the retry policy to use
      • DistributedAtomicLong

        public DistributedAtomicLong​(org.apache.curator.framework.CuratorFramework client,
                                     java.lang.String counterPath,
                                     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, a InterProcessMutex will be tried with its own retry policy
        Parameters:
        client - the client
        counterPath - path to hold the value
        retryPolicy - the retry policy to use
        promotedToLock - the arguments for the mutex promotion
    • Method Detail

      • get

        public AtomicValue<java.lang.Long> get()
                                        throws java.lang.Exception
        Description copied from interface: DistributedAtomicNumber
        Returns the current value of the counter. NOTE: if the value has never been set, 0 is returned.
        Specified by:
        get in interface DistributedAtomicNumber<java.lang.Long>
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • forceSet

        public void forceSet​(java.lang.Long newValue)
                      throws java.lang.Exception
        Description copied from interface: DistributedAtomicNumber
        Forcibly sets the value of the counter without any guarantees of atomicity.
        Specified by:
        forceSet in interface DistributedAtomicNumber<java.lang.Long>
        Parameters:
        newValue - the new value
        Throws:
        java.lang.Exception - ZooKeeper errors
      • compareAndSet

        public AtomicValue<java.lang.Long> compareAndSet​(java.lang.Long expectedValue,
                                                         java.lang.Long newValue)
                                                  throws java.lang.Exception
        Description copied from interface: DistributedAtomicNumber
        Atomically sets the value to the given updated value if the current value == the expected value. Remember to always check AtomicValue.succeeded().
        Specified by:
        compareAndSet in interface DistributedAtomicNumber<java.lang.Long>
        Parameters:
        expectedValue - the expected value
        newValue - the new value for the counter
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • trySet

        public AtomicValue<java.lang.Long> trySet​(java.lang.Long newValue)
                                           throws java.lang.Exception
        Description copied from interface: DistributedAtomicNumber
        Attempt to atomically set the value to the given value. Remember to always check AtomicValue.succeeded().
        Specified by:
        trySet in interface DistributedAtomicNumber<java.lang.Long>
        Parameters:
        newValue - the value to set
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • initialize

        public boolean initialize​(java.lang.Long initialize)
                           throws java.lang.Exception
        Description copied from interface: DistributedAtomicNumber
        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.
        Specified by:
        initialize in interface DistributedAtomicNumber<java.lang.Long>
        Parameters:
        initialize - the initial value to set
        Returns:
        true if the value was set, false if the node already existed
        Throws:
        java.lang.Exception - ZooKeeper errors
      • increment

        public AtomicValue<java.lang.Long> increment()
                                              throws java.lang.Exception
        Add 1 to the current value and return the new value information. Remember to always check AtomicValue.succeeded().
        Specified by:
        increment in interface DistributedAtomicNumber<java.lang.Long>
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • decrement

        public AtomicValue<java.lang.Long> decrement()
                                              throws java.lang.Exception
        Subtract 1 from the current value and return the new value information. Remember to always check AtomicValue.succeeded().
        Specified by:
        decrement in interface DistributedAtomicNumber<java.lang.Long>
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • add

        public AtomicValue<java.lang.Long> add​(java.lang.Long delta)
                                        throws java.lang.Exception
        Add delta to the current value and return the new value information. Remember to always check AtomicValue.succeeded().
        Specified by:
        add in interface DistributedAtomicNumber<java.lang.Long>
        Parameters:
        delta - amount to add
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors
      • subtract

        public AtomicValue<java.lang.Long> subtract​(java.lang.Long delta)
                                             throws java.lang.Exception
        Subtract delta from the current value and return the new value information. Remember to always check AtomicValue.succeeded().
        Specified by:
        subtract in interface DistributedAtomicNumber<java.lang.Long>
        Parameters:
        delta - amount to subtract
        Returns:
        value info
        Throws:
        java.lang.Exception - ZooKeeper errors