Curator getting results asynchronously

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.