The Zookeeper Crud API for the OST has synchronous and asynchronous points, and for asynchronous APIs, the AsyncCallback callback needs to be passed. For getdata,getchildren,exists These three APIs, you can also set watcher. How are these features implemented in curator?
In curator, you can get results asynchronously in the following three ways:
1.inBackground () +curatorlistener
2.inBackground (New Backgroundcallback () {public void Processresult (Curatorframework client,curatorevent event) {}})
3.inBackground (Newbackgroundcallback () {},executor)
Use the asynchronous API in Inbackground () +curatorlistener, as follows:
Client.getcuratorlistenable (). AddListener (NewCuratorlistener () {@Override Public voidEventreceived (curatorframework client, curatorevent event)throwsException {//TODO auto-generated Method Stub if(Event.gettype () = =curatoreventtype.create) {System.out.println ("Create Path=" +event.getpath () + ", code=" +Event.getresultcode ()); }Else if(Event.gettype () = =curatoreventtype.get_data) {System.out.println ("Get Path=" +event.getpath () + ", data=" +NewString (Event.getdata ())); }Else if(Event.gettype () = =curatoreventtype.set_data) {System.out.println ("Set Path=" +event.getpath () + ", data=" +NewString (Client.getdata (). Forpath (Event.getpath ()))/*+ ", data=" +new String (Event.getdata ())*/); }Else if(Event.gettype () = =curatoreventtype.delete) {System.out.println ("Delete path=" +Event.getpath ()); } }});
All of the following APIs, which are used by the client in Inbackground (), are processed by this curatorlistener for asynchronous processing.
In the second mode Inbackground (Backgroundcallback), the following:
client.create (). creatingparentsifneeded (). Withprotection (). Withmode (Createmode.eph emeral). Inbackground (NewBackgroundcallback () {@Override Public voidProcessresult (curatorframework client, curatorevent event)throwsException {//TODO auto-generated Method Stub if(Event.gettype () = =curatoreventtype.create) {System.out.println ("Code:" +event.getresultcode () + "path:" +Event.getpath ()); //client.getdata (). Inbackground (). Forpath (Event.getpath ());}}}). Forpath ("/francis/tmp/a", "WBS". GetBytes ());
Curator getting results asynchronously