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; }
}
}