org.I0Itec.zkclient
Class ZkClient

java.lang.Object
  extended by org.I0Itec.zkclient.ZkClient
All Implemented Interfaces:
org.apache.zookeeper.Watcher

public class ZkClient
extends java.lang.Object
implements org.apache.zookeeper.Watcher

Abstracts the interaction with zookeeper and allows permanent (not just one time) watches on nodes in ZooKeeper


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.zookeeper.Watcher
org.apache.zookeeper.Watcher.Event
 
Field Summary
protected  org.I0Itec.zkclient.IZkConnection _connection
           
 
Constructor Summary
ZkClient(org.I0Itec.zkclient.IZkConnection connection)
           
ZkClient(org.I0Itec.zkclient.IZkConnection connection, int connectionTimeout)
           
ZkClient(org.I0Itec.zkclient.IZkConnection zkConnection, int connectionTimeout, ZkSerializer zkSerializer)
           
ZkClient(java.lang.String serverstring)
           
ZkClient(java.lang.String zkServers, int connectionTimeout)
           
ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout)
           
ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer)
           
 
Method Summary
 void close()
          Close the client.
 void connect(long maxMsToWaitUntilConnected, org.apache.zookeeper.Watcher watcher)
          Connect to ZooKeeper.
 int countChildren(java.lang.String path)
          Counts number of children for the given path.
 java.lang.String create(java.lang.String path, java.lang.Object data, org.apache.zookeeper.CreateMode mode)
          Create a node.
 void createEphemeral(java.lang.String path)
          Create an ephemeral node.
 void createEphemeral(java.lang.String path, java.lang.Object data)
          Create an ephemeral node.
 java.lang.String createEphemeralSequential(java.lang.String path, java.lang.Object data)
          Create an ephemeral, sequential node.
 void createPersistent(java.lang.String path)
          Create a persistent node.
 void createPersistent(java.lang.String path, boolean createParents)
          Create a persistent node.
 void createPersistent(java.lang.String path, java.lang.Object data)
          Create a persistent node.
 java.lang.String createPersistentSequential(java.lang.String path, java.lang.Object data)
          Create a persistent, sequental node.
 boolean delete(java.lang.String path)
           
 boolean deleteRecursive(java.lang.String path)
           
 boolean exists(java.lang.String path)
           
protected  boolean exists(java.lang.String path, boolean watch)
           
 java.util.List<java.lang.String> getChildren(java.lang.String path)
           
protected  java.util.List<java.lang.String> getChildren(java.lang.String path, boolean watch)
           
 long getCreationTime(java.lang.String path)
           
protected  java.util.Set<IZkDataListener> getDataListener(java.lang.String path)
           
 ZkLock getEventLock()
          Returns a mutex all zookeeper events are synchronized aginst.
 boolean getShutdownTrigger()
           
 int numberOfListeners()
           
 void process(org.apache.zookeeper.WatchedEvent event)
           
<T> T
readData(java.lang.String path)
           
<T> T
readData(java.lang.String path, boolean returnNullIfPathNotExists)
           
<T> T
readData(java.lang.String path, org.apache.zookeeper.data.Stat stat)
           
protected
<T> T
readData(java.lang.String path, org.apache.zookeeper.data.Stat stat, boolean watch)
           
<T> T
retryUntilConnected(java.util.concurrent.Callable<T> callable)
           
 void setCurrentState(org.apache.zookeeper.Watcher.Event.KeeperState currentState)
           
 void setShutdownTrigger(boolean triggerState)
           
 void setZkSerializer(ZkSerializer zkSerializer)
           
 void showFolders(java.io.OutputStream output)
           
 java.util.List<java.lang.String> subscribeChildChanges(java.lang.String path, IZkChildListener listener)
           
 void subscribeDataChanges(java.lang.String path, IZkDataListener listener)
           
 void subscribeStateChanges(IZkStateListener listener)
           
 void unsubscribeAll()
           
 void unsubscribeChildChanges(java.lang.String path, IZkChildListener childListener)
           
 void unsubscribeDataChanges(java.lang.String path, IZkDataListener dataListener)
           
 void unsubscribeStateChanges(IZkStateListener stateListener)
           
<T> void
updateDataSerialized(java.lang.String path, DataUpdater<T> updater)
          Updates data of an existing znode.
 boolean waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState keeperState, long time, java.util.concurrent.TimeUnit timeUnit)
           
 void waitUntilConnected()
           
 boolean waitUntilConnected(long time, java.util.concurrent.TimeUnit timeUnit)
           
 boolean waitUntilExists(java.lang.String path, java.util.concurrent.TimeUnit timeUnit, long time)
           
 java.util.List<java.lang.String> watchForChilds(java.lang.String path)
          Installs a child watch for the given path.
 void watchForData(java.lang.String path)
           
 void writeData(java.lang.String path, java.lang.Object object)
           
 void writeData(java.lang.String path, java.lang.Object datat, int expectedVersion)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_connection

protected org.I0Itec.zkclient.IZkConnection _connection
Constructor Detail

ZkClient

public ZkClient(java.lang.String serverstring)

ZkClient

public ZkClient(java.lang.String zkServers,
                int connectionTimeout)

ZkClient

public ZkClient(java.lang.String zkServers,
                int sessionTimeout,
                int connectionTimeout)

ZkClient

public ZkClient(java.lang.String zkServers,
                int sessionTimeout,
                int connectionTimeout,
                ZkSerializer zkSerializer)

ZkClient

public ZkClient(org.I0Itec.zkclient.IZkConnection connection)

ZkClient

public ZkClient(org.I0Itec.zkclient.IZkConnection connection,
                int connectionTimeout)

ZkClient

public ZkClient(org.I0Itec.zkclient.IZkConnection zkConnection,
                int connectionTimeout,
                ZkSerializer zkSerializer)
Method Detail

setZkSerializer

public void setZkSerializer(ZkSerializer zkSerializer)

subscribeChildChanges

public java.util.List<java.lang.String> subscribeChildChanges(java.lang.String path,
                                                              IZkChildListener listener)

unsubscribeChildChanges

public void unsubscribeChildChanges(java.lang.String path,
                                    IZkChildListener childListener)

subscribeDataChanges

public void subscribeDataChanges(java.lang.String path,
                                 IZkDataListener listener)

unsubscribeDataChanges

public void unsubscribeDataChanges(java.lang.String path,
                                   IZkDataListener dataListener)

subscribeStateChanges

public void subscribeStateChanges(IZkStateListener listener)

unsubscribeStateChanges

public void unsubscribeStateChanges(IZkStateListener stateListener)

unsubscribeAll

public void unsubscribeAll()

createPersistent

public void createPersistent(java.lang.String path)
                      throws ZkInterruptedException,
                             java.lang.IllegalArgumentException,
                             ZkException,
                             java.lang.RuntimeException
Create a persistent node.

Parameters:
path -
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

createPersistent

public void createPersistent(java.lang.String path,
                             boolean createParents)
                      throws ZkInterruptedException,
                             java.lang.IllegalArgumentException,
                             ZkException,
                             java.lang.RuntimeException
Create a persistent node.

Parameters:
path -
createParents - if true all parent dirs are created as well and no ZkNodeExistsException is thrown in case the path already exists
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

createPersistent

public void createPersistent(java.lang.String path,
                             java.lang.Object data)
                      throws ZkInterruptedException,
                             java.lang.IllegalArgumentException,
                             ZkException,
                             java.lang.RuntimeException
Create a persistent node.

Parameters:
path -
data -
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

createPersistentSequential

public java.lang.String createPersistentSequential(java.lang.String path,
                                                   java.lang.Object data)
                                            throws ZkInterruptedException,
                                                   java.lang.IllegalArgumentException,
                                                   ZkException,
                                                   java.lang.RuntimeException
Create a persistent, sequental node.

Parameters:
path -
data -
Returns:
create node's path
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

createEphemeral

public void createEphemeral(java.lang.String path)
                     throws ZkInterruptedException,
                            java.lang.IllegalArgumentException,
                            ZkException,
                            java.lang.RuntimeException
Create an ephemeral node.

Parameters:
path -
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

create

public java.lang.String create(java.lang.String path,
                               java.lang.Object data,
                               org.apache.zookeeper.CreateMode mode)
                        throws ZkInterruptedException,
                               java.lang.IllegalArgumentException,
                               ZkException,
                               java.lang.RuntimeException
Create a node.

Parameters:
path -
data -
mode -
Returns:
create node's path
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

createEphemeral

public void createEphemeral(java.lang.String path,
                            java.lang.Object data)
                     throws ZkInterruptedException,
                            java.lang.IllegalArgumentException,
                            ZkException,
                            java.lang.RuntimeException
Create an ephemeral node.

Parameters:
path -
data -
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

createEphemeralSequential

public java.lang.String createEphemeralSequential(java.lang.String path,
                                                  java.lang.Object data)
                                           throws ZkInterruptedException,
                                                  java.lang.IllegalArgumentException,
                                                  ZkException,
                                                  java.lang.RuntimeException
Create an ephemeral, sequential node.

Parameters:
path -
data -
Returns:
created path
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs

process

public void process(org.apache.zookeeper.WatchedEvent event)
Specified by:
process in interface org.apache.zookeeper.Watcher

getChildren

public java.util.List<java.lang.String> getChildren(java.lang.String path)

getChildren

protected java.util.List<java.lang.String> getChildren(java.lang.String path,
                                                       boolean watch)

countChildren

public int countChildren(java.lang.String path)
Counts number of children for the given path.

Parameters:
path -
Returns:
number of children or 0 if path does not exist.

exists

protected boolean exists(java.lang.String path,
                         boolean watch)

exists

public boolean exists(java.lang.String path)

deleteRecursive

public boolean deleteRecursive(java.lang.String path)

waitUntilExists

public boolean waitUntilExists(java.lang.String path,
                               java.util.concurrent.TimeUnit timeUnit,
                               long time)
                        throws ZkInterruptedException
Throws:
ZkInterruptedException

getDataListener

protected java.util.Set<IZkDataListener> getDataListener(java.lang.String path)

showFolders

public void showFolders(java.io.OutputStream output)

waitUntilConnected

public void waitUntilConnected()
                        throws ZkInterruptedException
Throws:
ZkInterruptedException

waitUntilConnected

public boolean waitUntilConnected(long time,
                                  java.util.concurrent.TimeUnit timeUnit)
                           throws ZkInterruptedException
Throws:
ZkInterruptedException

waitForKeeperState

public boolean waitForKeeperState(org.apache.zookeeper.Watcher.Event.KeeperState keeperState,
                                  long time,
                                  java.util.concurrent.TimeUnit timeUnit)
                           throws ZkInterruptedException
Throws:
ZkInterruptedException

retryUntilConnected

public <T> T retryUntilConnected(java.util.concurrent.Callable<T> callable)
                      throws ZkInterruptedException,
                             java.lang.IllegalArgumentException,
                             ZkException,
                             java.lang.RuntimeException
Type Parameters:
T -
Parameters:
callable -
Returns:
result of Callable
Throws:
ZkInterruptedException - if operation was interrupted, or a required reconnection got interrupted
java.lang.IllegalArgumentException - if called from anything except the ZooKeeper event thread
ZkException - if any ZooKeeper exception occurred
java.lang.RuntimeException - if any other exception occurs from invoking the Callable

setCurrentState

public void setCurrentState(org.apache.zookeeper.Watcher.Event.KeeperState currentState)

getEventLock

public ZkLock getEventLock()
Returns a mutex all zookeeper events are synchronized aginst. So in case you need to do something without getting any zookeeper event interruption synchronize against this mutex. Also all threads waiting on this mutex object will be notified on an event.

Returns:
the mutex.

delete

public boolean delete(java.lang.String path)

readData

public <T> T readData(java.lang.String path)

readData

public <T> T readData(java.lang.String path,
                      boolean returnNullIfPathNotExists)

readData

public <T> T readData(java.lang.String path,
                      org.apache.zookeeper.data.Stat stat)

readData

protected <T> T readData(java.lang.String path,
                         org.apache.zookeeper.data.Stat stat,
                         boolean watch)

writeData

public void writeData(java.lang.String path,
                      java.lang.Object object)

updateDataSerialized

public <T> void updateDataSerialized(java.lang.String path,
                                     DataUpdater<T> updater)
Updates data of an existing znode. The current content of the znode is passed to the DataUpdater that is passed into this method, which returns the new content. The new content is only written back to ZooKeeper if nobody has modified the given znode in between. If a concurrent change has been detected the new data of the znode is passed to the updater once again until the new contents can be successfully written back to ZooKeeper.

Type Parameters:
T -
Parameters:
path - The path of the znode.
updater - Updater that creates the new contents.

writeData

public void writeData(java.lang.String path,
                      java.lang.Object datat,
                      int expectedVersion)

watchForData

public void watchForData(java.lang.String path)

watchForChilds

public java.util.List<java.lang.String> watchForChilds(java.lang.String path)
Installs a child watch for the given path.

Parameters:
path -
Returns:
the current children of the path or null if the zk node with the given path doesn't exist.

connect

public void connect(long maxMsToWaitUntilConnected,
                    org.apache.zookeeper.Watcher watcher)
             throws ZkInterruptedException,
                    ZkTimeoutException,
                    java.lang.IllegalStateException
Connect to ZooKeeper.

Parameters:
maxMsToWaitUntilConnected -
watcher -
Throws:
ZkInterruptedException - if the connection timed out due to thread interruption
ZkTimeoutException - if the connection timed out
java.lang.IllegalStateException - if the connection timed out due to thread interruption

getCreationTime

public long getCreationTime(java.lang.String path)

close

public void close()
           throws ZkInterruptedException
Close the client.

Throws:
ZkInterruptedException

setShutdownTrigger

public void setShutdownTrigger(boolean triggerState)

getShutdownTrigger

public boolean getShutdownTrigger()

numberOfListeners

public int numberOfListeners()