com.netflix.curator.framework.recipes.shared
Class SharedCount

java.lang.Object
  extended by com.netflix.curator.framework.recipes.shared.SharedCount
All Implemented Interfaces:
com.netflix.curator.framework.listen.Listenable<SharedCountListener>, SharedCountReader, java.io.Closeable

public class SharedCount
extends java.lang.Object
implements java.io.Closeable, SharedCountReader, com.netflix.curator.framework.listen.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 Summary
SharedCount(com.netflix.curator.framework.CuratorFramework client, java.lang.String path, int seedValue)
           
 
Method Summary
 void addListener(SharedCountListener listener)
           
 void addListener(SharedCountListener listener, java.util.concurrent.Executor executor)
           
 void close()
           
 int getCount()
          Return the current value of the count
 void removeListener(SharedCountListener listener)
           
 void setCount(int newCount)
          Change the shared count value irrespective of its previous state
 void start()
          The shared count must be started before it can be used.
 boolean trySetCount(int newCount)
          Changes the shared count only if its value has not changed since this client last read it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SharedCount

public SharedCount(com.netflix.curator.framework.CuratorFramework client,
                   java.lang.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
Method Detail

getCount

public int getCount()
Description copied from interface: SharedCountReader
Return the current value of the count

Specified by:
getCount in interface SharedCountReader
Returns:
count

setCount

public void setCount(int newCount)
              throws java.lang.Exception
Change the shared count value irrespective of its previous state

Parameters:
newCount - new value
Throws:
java.lang.Exception - ZK errors, interruptions, etc.

trySetCount

public boolean trySetCount(int newCount)
                    throws java.lang.Exception
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:
java.lang.Exception - ZK errors, interruptions, etc.

addListener

public void addListener(SharedCountListener listener)
Specified by:
addListener in interface com.netflix.curator.framework.listen.Listenable<SharedCountListener>

addListener

public void addListener(SharedCountListener listener,
                        java.util.concurrent.Executor executor)
Specified by:
addListener in interface com.netflix.curator.framework.listen.Listenable<SharedCountListener>

removeListener

public void removeListener(SharedCountListener listener)
Specified by:
removeListener in interface com.netflix.curator.framework.listen.Listenable<SharedCountListener>

start

public void start()
           throws java.lang.Exception
The shared count must be started before it can be used. Call close() when you are finished with the shared count

Throws:
java.lang.Exception - ZK errors, interruptions, etc.

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException