Read the rest of that web page.

Read this exchange as well:
http://zookeeper-user.578899.n2.nabble.com/Persistent-watch-stream-td5734685.html

<http://zookeeper-user.578899.n2.nabble.com/Persistent-watch-stream-td5734685.html>Do
a google search and summarize what you find in your next question.

2011/6/21 lei liu <[email protected]>

> Hi Ted, thank you for your reply.
>
> Can you tell me why I need to reset the watch? I think that is confusing.
>
> Thanks,
>
> LiuLei
>
>
> 2011/6/21 Ted Dunning <[email protected]>
>
> > Note the part about "and clear the watch"
> >
> >
> >
> http://zookeeper.apache.org/doc/r3.1.2/zookeeperProgrammers.html#sc_zkDataMode_watches
> >
> > your process method must reset the watch.  This behavior is intentional
> and
> > quite important for ZK to work correctly.
> >
> > 2011/6/21 lei liu <[email protected]>
> >
> > > I use Zookeeper 3.3.3 version.
> > >
> > > I create "test" znode, and I add many children for the znode. I want to
> > > listen EventType.NodeChildrenChanged event, when there is
> > > EventType.NodeChildrenChanged event, I print the event information,
> > example
> > > below code:
> > >
> > > public class DisplayGroup implements Watcher {
> > >
> > >    private static final int SESSION_TIMEOUT = 5000;
> > >
> > >    protected ZooKeeper zk;
> > >
> > >    public void connect(String hosts) throws IOException,
> > > InterruptedException {
> > >        zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
> > >
> > >    }
> > >
> > >    @Override
> > >    public void process(WatchedEvent event) {
> > >
> > >        System.out.println(this.getClass().getName() + ": " + event);
> > >    }
> > >
> > >    public void close() throws InterruptedException {
> > >        zk.close();
> > >    }
> > >
> > >    public void list(String groupName) throws KeeperException,
> > >            InterruptedException {
> > >        String path = "/" + groupName;
> > >        try {
> > >
> > >            List<String> children = zk.getChildren(path, true);
> > >            if (children.isEmpty()) {
> > >                System.out.printf("No members in group %s\n",
> groupName);
> > >
> > >            }
> > >            for (String child : children) {
> > >                System.out.println(child);
> > >            }
> > >        } catch (KeeperException.NoNodeException e) {
> > >            System.out.printf("Group %s does not exist\n", groupName);
> > >
> > >        }
> > >    }
> > >
> > >    public static void main(String[] args) throws Exception {
> > >        DisplayGroup listGroup = new DisplayGroup();
> > >        String connectString = "localhost:2181";
> > >        listGroup.connect(connectString);
> > >        listGroup.list("test");
> > >        Thread.sleep(Long.MAX_VALUE);
> > >    }
> > >
> > > }
> > >
> > >
> > > I add many children to the "test" znode, but the process() method of
> > > DisplayGroup  calss only is called one time, the output information is:
> > > 1308621717188
> > > 1308621719250
> > > examples.DisplayGroup: WatchedEvent state:SyncConnected
> > > type:NodeChildrenChanged path:/test
> > >
> > >
> > >
> > > I want to know why the process() method of DisplayGroup  calss is not
> > > called
> > > many times when I add many children to the "test" znode?
> > >
> > >
> > > Thanks,
> > >
> > >
> > > LiuLei
> > >
> >
>

Reply via email to