Class SharedCount

java.lang.Object
org.apache.curator.framework.recipes.shared.SharedCount
All Implemented Interfaces:
Closeable, AutoCloseable, Listenable<SharedCountListener>, SharedCountReader

public class SharedCount extends Object implements Closeable, SharedCountReader, Listenable<SharedCountListener>
Manages a shared integer. All clients watching the same path will have the up-to-date value of the shared integer (considering ZK's normal consistency guarantees).
  • Constructor Details

    • SharedCount

      public SharedCount(CuratorFramework client, String path, int seedValue)
      Parameters:
      client - the client
      path - the shared path - i.e. where the shared count is stored
      seedValue - the initial value for the count if/f the path has not yet been created
    • SharedCount

      protected SharedCount(CuratorFramework client, String path, SharedValue sv)
  • Method Details

    • getCount

      public int getCount()
      Description copied from interface: SharedCountReader
      Return the current value of the count
      Specified by:
      getCount in interface SharedCountReader
      Returns:
      count
    • getVersionedValue

      public VersionedValue<Integer> getVersionedValue()
      Description copied from interface: SharedCountReader
      Return the current count and version
      Specified by:
      getVersionedValue in interface SharedCountReader
      Returns:
      count and version
    • setCount

      public void setCount(int newCount) throws Exception
      Change the shared count value irrespective of its previous state
      Parameters:
      newCount - new value
      Throws:
      Exception - ZK errors, interruptions, etc.
    • trySetCount

      @Deprecated public boolean trySetCount(int newCount) throws Exception
      Deprecated.
      use trySetCount(VersionedValue, int) for stronger atomicity guarantees. Even if this object's internal state is up-to-date, the caller has no way to ensure that they've read the most recently seen count.
      Changes the shared count only if its value has not changed since this client last read it. If the count has changed, the value is not set and this client's view of the value is updated. i.e. if the count is not successful you can get the updated value by calling getCount().
      Parameters:
      newCount - the new value to attempt
      Returns:
      true if the change attempt was successful, false if not. If the change was not successful, getCount() will return the updated value
      Throws:
      Exception - ZK errors, interruptions, etc.
    • trySetCount

      public boolean trySetCount(VersionedValue<Integer> previous, int newCount) throws Exception
      Changes the shared count only if its value has not changed since the version specified by newCount. If the count has changed, the value is not set and this client's view of the value is updated. i.e. if the count is not successful you can get the updated value by calling getCount().
      Parameters:
      newCount - the new value to attempt
      Returns:
      true if the change attempt was successful, false if not. If the change was not successful, getCount() will return the updated value
      Throws:
      Exception - ZK errors, interruptions, etc.
    • addListener

      public void addListener(SharedCountListener listener)
      Description copied from interface: Listenable
      Add the given listener. The listener will be executed in the containing instance's thread.
      Specified by:
      addListener in interface Listenable<SharedCountListener>
      Parameters:
      listener - listener to add
    • addListener

      public void addListener(SharedCountListener listener, Executor executor)
      Description copied from interface: Listenable
      Add the given listener. The listener will be executed using the given executor
      Specified by:
      addListener in interface Listenable<SharedCountListener>
      Parameters:
      listener - listener to add
      executor - executor to run listener in
    • removeListener

      public void removeListener(SharedCountListener listener)
      Description copied from interface: Listenable
      Remove the given listener
      Specified by:
      removeListener in interface Listenable<SharedCountListener>
      Parameters:
      listener - listener to remove
    • start

      public void start() throws Exception
      The shared count must be started before it can be used. Call close() when you are finished with the shared count
      Throws:
      Exception - ZK errors, interruptions, etc.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException