hadoop-zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benjamin Reed <br...@yahoo-inc.com>
Subject RE: Updated NodeWatcher...
Date Thu, 08 Jan 2009 22:55:00 GMT
Hey kevin,

i just found that part of this thread went to my junk folder. can you send the URL for the

this NodeWatcher is a useful thing. i have a couple of suggestions to simplify it:

1) Construct the NodeWatcher with a ZooKeeper object rather than constructing one. Not only
does it simplify NodeWatcher, but it also makes it so that the ZooKeeper object can be used
for other things as well.
2) Use the async API in watchNodeData and watchNodeExists. it simplifies the code and the
error handling.
3) You don't need to do a connect() in handleDisconnected(). ZooKeeper object will do it automatically
for you.

There is an old example on sourceforge http://zookeeper.wiki.sourceforge.net/ZooKeeperJavaExample
that may give you some more ideas on how to simplify your code.

From: burtonator@gmail.com [burtonator@gmail.com] On Behalf Of Kevin Burton [burton@spinn3r.com]
Sent: Thursday, January 08, 2009 12:00 AM
To: zookeeper-user@hadoop.apache.org
Subject: Updated NodeWatcher...

I've updated based on the last round of feedback regarding ZK operation:

This is what I think the API should look like for event based applications
without any insane requirements.

You write code like:

        NodeListener listener = new NodeListener() {

                public void onData( String path, Stat stat, byte[] data ) {
                    System.out.printf( "event: onData path: %s, version: %s,
data: %s\n", path, stat.getVersion(), new String( data ) );


                public void onKeeperException( KeeperException e ) {
                    System.out.printf( "event: onKeeperException: \n" );

                public void onFailure() {
                    System.out.printf( "event: onFailure\n" );

                public void onConnect() {
                    System.out.printf( "event: onConnect\n" );


        NodeWatcher nWatcher = new NodeWatcher( "server45.example.com:2181,
server46.example.com:2181,server47.example.com:2181", listener );
        nWatcher.watch( "/foo" );


.... and then you will receive events back from ZK when the files are

I believe it handles all race and fail conditions of ZK which was the hard
part since some of these are subtle or not really documented.

I'd be willing to contrib this if others found it useful.

I think we're going to use it as our main interface to ZK since it solves
all the issues I care about.


Founder/CEO Spinn3r.com
Location: San Francisco, CA
AIM/YIM: sfburtonator
Skype: burtonator
Work: http://spinn3r.com

View raw message