The curator framework determines whether the zookeeper node is a temporary node or a persisted node __zookeeper

Source: Internet
Author: User
Tags zookeeper zookeeper client

Curator Framework client use, please see a blog "Zookeeper client curator framework use".

The Curatorframework class has an interface that determines whether a node exists checkexists (), which returns a Org.apache.zookeeper.data.Stat object that has a Ephemeralowner attribute in the object.

If the node is a persisted node, the value of Ephemeralowner is 0 if the node is a temporary node, the value of Ephemeralowner is greater than 0

Example, there is now a persistent node (/chijiu) and a temporary node (/linshi) on the zookeeper.

1. Get persistent node (/CHIJIU) node Stat object


2. Get temporary node (/linshi) Node Stat object


The complete code is as follows:

Introducing Jar Packs


Test code

Import Org.apache.curator.RetryPolicy;
Import Org.apache.curator.framework.CuratorFramework;
Import Org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.retry.ExponentialBackoffRetry;
Import Org.apache.zookeeper.CreateMode;

Import Org.apache.zookeeper.data.Stat;
 /** * Created by Wzj on 2018/5/3. */public class Zktest {public static void main (string[] args) throws Exception {Retrypolicy retrypolic

        y = new Exponentialbackoffretry (1000, 3);

        Curatorframework client = curatorframeworkfactory.newclient ("192.168.3.45:2181", Retrypolicy);


        Client.start ();
        Cratenode (client, "/linshi", Createmode.ephemeral, "");

        Cratenode (client, "/chijiu", Createmode.persistent, "");

        SYSTEM.OUT.PRINTLN (Getnodetype (client, "/linshi"));
    SYSTEM.OUT.PRINTLN (Getnodetype (client, "/chijiu")); /** * Create node * @param client clients * @param path PATH * @param createmode node type * @param da       TaNode Data * @return Whether to create success/public static Boolean Cratenode (curatorframework client,string path, Createmode CRE Atemode, String data) {try {client.create (). Withmode (Createmode). Forpath (Path, DATA.GETB
        Ytes ());
            catch (Exception e) {e.printstacktrace ();
        return false;
    return true; /** * To determine if the node is a persistent node * @param client clients * @param path PATH * @return null-node does not exist | createmode.persistent-is Persistent |
        createmode.ephemeral-Temporary node */public static Createmode Getnodetype (curatorframework client,string path) {

            try {Stat Stat = client.checkexists (). Forpath (path);
            if (stat = = null) {return null;
            } if (Stat.getephemeralowner () > 0) {return createmode.ephemeral;
        return createmode.persistent;
    }    catch (Exception e) {e.printstacktrace ();
        return null; }
    }

}

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.