zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 沈冠璞 <shengua...@360.cn>
Subject Re: getData return result between Watcher process?
Date Tue, 21 Oct 2014 11:34:22 GMT
thanks very much for your explain.
what I still puzzled is
public KeeperState getState() {
        return keeperState;

    public EventType getType() {
        return eventType;

    public String getPath() {
        return path;

the three method should be return quickly  ,but  the thread showed interrupt ,the main thread
print   123 , I think it is wired.
奇虎360  PC网游事业部
Tony Horton语录:Talk is cheap. Show me the abs.
6楼A3   100015


在 2014年10月21日,上午11:01,Jürgen Wagner (DVT) <juergen.wagner@devoteam.com<mailto:juergen.wagner@devoteam.com>>

  Zookeeper works asynchronously in several threads. Therefore the sequence of execution in
different threads is not generally predictable. It could therefore happen that when the connection
status change is detected, the Watcher is executed, but only the first "hello zookeeper" gets
echoed, then the main thread gets some cycles again and prints "123", after which the second
print statement "hello event!..." is executed. If you don't want this to happen, use a CountDownLatch
to make the main thread wait until the Zookeeper connection is established and propertly recognized
in your program. The main thread creates the CountDownLatch(1), opens the Zk connection and
waits latch.await(). The Watcher does its job and then counts the latch down by one, causing
the main thread to leave the await and continue doing its job.

Best regards,

On 21.10.2014 04:40, 沈冠璞 wrote:

thanks for your reply.
but i means  the order should be :
hello zookeeper
hello event! type=None, stat=SyncConnected, path=null

but the real result order is :
hello zookeeper
hello event! type=None, stat=SyncConnected, path=null

Best Regards!
Guanpu Shen

在 2014年10月21日,上午9:07,Biju N <bijuatapache@gmail.com<mailto:bijuatapache@gmail.com><mailto:bijuatapache@gmail.com><mailto:bijuatapache@gmail.com>>

The "System.out.println(new String(zk.getData(znode,false,null)));" is
causing 123 to be displayed.

On Fri, Oct 17, 2014 at 12:15 AM, 沈冠璞 <shenguanpu@360.cn<mailto:shenguanpu@360.cn><mailto:shenguanpu@360.cn><mailto:shenguanpu@360.cn>>

hi, I am a newbie of the zookeeper.I have a question on get data order.
here is my program:

public class ZkReader {
   public static void main(String[] args) throws IOException,
InterruptedException, KeeperException {
       String hostPort = ",,";
       String znode = "/test";
       ZooKeeper zk = new ZooKeeper(hostPort, 3000, new MyWatcher());
       System.out.println(new String(zk.getData(znode,false,null)));

class MyWatcher  implements Watcher {

   public void process(WatchedEvent event) {
       System.out.println("hello zookeeper");
       System.out.println(String.format("hello event! type=%s, stat=%s,

the result:
hello zookeeper
hello event! type=None, stat=SyncConnected, path=null

I don’t know why  the data of node /test: 123 , shows between  "hello
zookeeper" and "hello event!”
Could somebody explain for me? Thanks a lot.

Best Regards!
Guanpu Shen


Mit freundlichen Grüßen/Kind regards/Cordialement vôtre/Atentamente/С уважением
i.A. Jürgen Wagner
Head of Competence Center "Intelligence"
& Senior Cloud Consultant

Devoteam GmbH, Industriestr. 3, 70565 Stuttgart, Germany
Phone: +49 6151 868-8725, Fax: +49 711 13353-53, Mobile: +49 171 864 1543
E-Mail: juergen.wagner@devoteam.com<mailto:juergen.wagner@devoteam.com>, URL: www.devoteam.de<http://www.devoteam.de/>

Managing Board: Jürgen Hatzipantelis (CEO)
Address of Record: 64331 Weiterstadt, Germany; Commercial Register: Amtsgericht Darmstadt
HRB 6450; Tax Number: DE 172 993 071

  • Unnamed multipart/related (inline, None, 0 bytes)
View raw message