Database Reference
In-Depth Information
try {
List < String > children = zk . getChildren ( path , false );
if ( children . isEmpty ()) {
System . out . printf ( "No members in group %s\n" , groupName );
System . exit ( 1 );
}
for ( String child : children ) {
System . out . println ( child );
}
} catch ( KeeperException . NoNodeException e ) {
System . out . printf ( "Group %s does not exist\n" , groupName );
System . exit ( 1 );
}
}
public static void main ( String [] args ) throws Exception {
ListGroup listGroup = new ListGroup ();
listGroup . connect ( args [ 0 ]);
listGroup . list ( args [ 1 ]);
listGroup . close ();
}
}
In the list() method, we call getChildren() with a znode path and a watch flag to
retrieve a list of child paths for the znode, which we print out. Placing a watch on a znode
causes the registered Watcher to be triggered if the znode changes state. Although we're
not using it here, watching a znode's children would permit a program to get notifications
of members joining or leaving the group, or of the group being deleted.
We catch KeeperException.NoNodeException , which is thrown in the case
when the group's znode does not exist.
Let's see ListGroup in action. As expected, the zoo group is empty, since we haven't
added any members yet:
% java ListGroup localhost zoo
No members in group zoo
We can use JoinGroup to add some members. We launch them as background pro-
cesses, since they don't terminate on their own (due to the sleep statement):
% java JoinGroup localhost zoo duck &
% java JoinGroup localhost zoo cow &
% java JoinGroup localhost zoo goat &
% goat_pid=$!
Search WWH ::




Custom Search