Use of the curator API

Source: Internet
Author: User

Zookeeper is cumbersome for clients on Java, and watcher can only be triggered once. So with Zkclient and curator. The curator feature is that you can use the fluent style to write code.

Today the curator API is a simple use of a bit.

1.curator operation of the node requires curator-framework jar package

<dependency> <groupId>org.apache.curator</groupId> <artifactid>curator-frame Work</artifactid> <version>2.8.0</version> </dependency>
  (String[] args)  {        string servers =  retrypolicy retrypolicy = exponentialbackoffretry () curatorframework curator =  curatorframeworkfactory. ()                 . ConnectString (servers)                  .retrypolicy (Retrypolicy)                  .sessiontimeoutms ()                          .build () Curator.start () {             curator.create (). creatingParentsIfNeeded (). Inbackground (Backgroundcallback ()  {                  (curatorframework curatorframeworkcuratorevent curatorevent)  Exception  {                     system. println (curatorevent) System: println (Curatorframework)}            }). ForPath (. GetBytes ())}  (exception e)  {             e.printstacktrace ()}    }    mycallback backgroundcallback  {         (curatorframework curatorframeworkcuratorevent  curatorevent)  Exception {             system. println (curatorevent) System: println (Curatorframework)}    }


2. Monitoring of nodes and sub-nodes requires Curator-recipes jar report

org.apache.curatorcurator-recipes2.8.0
(String[] args)  {        String servers =  Retrypolicy retrypolicy = exponentialbackoffretry () curatorframework curator =  Curatorframeworkfactory. (Serversretrypolicy) Curator.start () {             Pathchildrencache childcache = pathchildrencache (curator) Childcache.start () Childcache.getlistenable (). AddListener (Pathchildrencachelistener ()  {                  (curatorframework  Curatorframeworkpathchildrencacheevent pathchildrencacheevent)  Exception {                     system. println () System: println (Pathchildrencacheevent.tostring ())}             }) timeunit. Sleep ()}&NBSP; (exception e)  {             E.printstacktrace ()}    }


Use of the curator API

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.