com.twitter.common.zookeeper
Class ZooKeeperNode<T>

java.lang.Object
  extended by com.twitter.common.zookeeper.ZooKeeperNode<T>
Type Parameters:
T - the type of data this node stores
All Implemented Interfaces:
Supplier<T>

public class ZooKeeperNode<T>
extends Object
implements Supplier<T>

An implementation of Supplier that offers a readonly view of a zookeeper data node. This class is thread-safe. Instances of this class each maintain a zookeeper watch for the remote data node. Instances of this class should be created via the create(com.twitter.common.zookeeper.ZooKeeperClient, java.lang.String, com.google.common.base.Function) factory method. Please see zookeeper documentation and talk to your cluster administrator for guidance on appropriate node size and total number of nodes you should be using.


Nested Class Summary
static interface ZooKeeperNode.NodeDeserializer<T>
          Interface for defining zookeeper node data deserialization.
 
Field Summary
static Function<byte[],byte[]> BYTE_ARRAY_VALUE
          Deserializer for the constructor if you want to simply store the zookeeper byte[] data as-is.
 
Method Summary
static
<T> ZooKeeperNode<T>
create(ZooKeeperClient zkClient, String nodePath, Function<byte[],T> deserializer)
          Returns an initialized ZooKeeperNode.
static
<T> ZooKeeperNode<T>
create(ZooKeeperClient zkClient, String nodePath, Function<byte[],T> deserializer, Closure<T> dataUpdateListener)
          Like the above, but optionally takes in a Closure that will get notified whenever the data is updated from the remote node.
static
<T> ZooKeeperNode<T>
create(ZooKeeperClient zkClient, String nodePath, ZooKeeperNode.NodeDeserializer<T> deserializer)
          Returns an initialized ZooKeeperNode.
static
<T> ZooKeeperNode<T>
create(ZooKeeperClient zkClient, String nodePath, ZooKeeperNode.NodeDeserializer<T> deserializer, Closure<T> dataUpdateListener)
          Like the above, but optionally takes in a Closure that will get notified whenever the data is updated from the remote node.
 T get()
          Returns the data corresponding to a byte array in a remote zookeeper node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTE_ARRAY_VALUE

public static final Function<byte[],byte[]> BYTE_ARRAY_VALUE
Deserializer for the constructor if you want to simply store the zookeeper byte[] data as-is.

Method Detail

create

public static <T> ZooKeeperNode<T> create(ZooKeeperClient zkClient,
                                          String nodePath,
                                          Function<byte[],T> deserializer)
                               throws InterruptedException,
                                      org.apache.zookeeper.KeeperException,
                                      ZooKeeperClient.ZooKeeperConnectionException
Returns an initialized ZooKeeperNode. The given node must exist at the time of object creation or a KeeperException will be thrown.

Parameters:
zkClient - a zookeeper client
nodePath - path to a node whose data will be watched
deserializer - a function that converts byte[] data from a zk node to this supplier's type T
Throws:
InterruptedException - if the underlying zookeeper server transaction is interrupted
KeeperException.NoNodeException - if the given nodePath doesn't exist
org.apache.zookeeper.KeeperException - if the server signals an error
ZooKeeperClient.ZooKeeperConnectionException - if there was a problem connecting to the zookeeper cluster

create

public static <T> ZooKeeperNode<T> create(ZooKeeperClient zkClient,
                                          String nodePath,
                                          Function<byte[],T> deserializer,
                                          Closure<T> dataUpdateListener)
                               throws InterruptedException,
                                      org.apache.zookeeper.KeeperException,
                                      ZooKeeperClient.ZooKeeperConnectionException
Like the above, but optionally takes in a Closure that will get notified whenever the data is updated from the remote node.

Parameters:
dataUpdateListener - a Closure to receive data update notifications.
Throws:
InterruptedException
org.apache.zookeeper.KeeperException
ZooKeeperClient.ZooKeeperConnectionException

create

public static <T> ZooKeeperNode<T> create(ZooKeeperClient zkClient,
                                          String nodePath,
                                          ZooKeeperNode.NodeDeserializer<T> deserializer)
                               throws InterruptedException,
                                      org.apache.zookeeper.KeeperException,
                                      ZooKeeperClient.ZooKeeperConnectionException
Returns an initialized ZooKeeperNode. The given node must exist at the time of object creation or a KeeperException will be thrown.

Parameters:
zkClient - a zookeeper client
nodePath - path to a node whose data will be watched
deserializer - an implentation of ZooKeeperNode.NodeDeserializer that converts a byte[] from a zk node to this supplier's type T. Also supplies a Stat object which is useful for doing versioned updates.
Throws:
InterruptedException - if the underlying zookeeper server transaction is interrupted
KeeperException.NoNodeException - if the given nodePath doesn't exist
org.apache.zookeeper.KeeperException - if the server signals an error
ZooKeeperClient.ZooKeeperConnectionException - if there was a problem connecting to the zookeeper cluster

create

public static <T> ZooKeeperNode<T> create(ZooKeeperClient zkClient,
                                          String nodePath,
                                          ZooKeeperNode.NodeDeserializer<T> deserializer,
                                          Closure<T> dataUpdateListener)
                               throws InterruptedException,
                                      org.apache.zookeeper.KeeperException,
                                      ZooKeeperClient.ZooKeeperConnectionException
Like the above, but optionally takes in a Closure that will get notified whenever the data is updated from the remote node.

Parameters:
dataUpdateListener - a Closure to receive data update notifications.
Throws:
InterruptedException
org.apache.zookeeper.KeeperException
ZooKeeperClient.ZooKeeperConnectionException

get

@Nullable
public T get()
Returns the data corresponding to a byte array in a remote zookeeper node. This data is cached locally and updated in the background on watch notifications.

Specified by:
get in interface Supplier<T>
Returns:
the data currently cached locally or null if init() hasn't been called or the backing node has no data or does not exist anymore.