Hello, 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, --Jürgen 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 > 123 > > but the real result order is : > hello zookeeper > 123 > hello event! type=None, stat=SyncConnected, path=null > > > > Best Regards! > Guanpu Shen > > 在 2014年10月21日,上午9:07,Biju N > <[email protected]<mailto:[email protected]>> 写道: > > 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, 沈冠璞 > <[email protected]<mailto:[email protected]>> wrote: > > 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 = "10.16.73.22,10.16.73.12,10.16.73.13"; > 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 { > > @Override > public void process(WatchedEvent event) { > System.out.println("hello zookeeper"); > System.out.println(String.format("hello event! type=%s, stat=%s, > path=%s",event.getType(),event.getState(),event.getPath())); > } > } > > the result: > hello zookeeper > 123 > 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: [email protected] <mailto:[email protected]>, 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
