/*****zookeeper Address ********/
Static final String connect_addr = "112.124.121.34:2181";
/*****session Timeout Time ********/
static final int session_outtime = 5000;
/**** blocking execution is used to wait for the zookeeper connection to successfully send a successful signal *******/
Static final Countdownlatch Connectedsemaphore = new Countdownlatch (1);
Static final Countdownlatch ConnectedSemaphore2 = new Countdownlatch (2);
public static void Main (string[] args) throws Exception {
ZooKeeper ZK = new ZooKeeper (connect_addr, Session_outtime, New Watcher () {
@Override
public void process (Watchedevent event) {
Get Event Status
Keeperstate keeperstate = Event.getstate ();
Gets the type of event
EventType EventType = Event.gettype ();
If you are establishing a connection
if (keeperstate.syncconnected = = keeperstate) {
if (Eventtype.none = = EventType) {
If successful, sends a signal that the block continues to execute downwards.
Connectedsemaphore.countdown ();
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
// }
System.out.println ("ZK establishes connection");
Connectedsemaphore.countdown ();
}
}
}
});
Blocking because the zookeeper is asynchronous and needs to be blocked
Connectedsemaphore.await ();
Get node
byte[] data = Zk.getdata ("/testroot", false, NULL);
System.out.println (new String (data));
System.out.println (Zk.getchildren ("/testroot", false));
list<string> list = Zk.getchildren ("/testroot", false);
for (String path:list) {
SYSTEM.OUT.PRINTLN (path);
String Realpath = "/testroot/" +path;
System.out.println (New String (Zk.getdata (Realpath, FALSE, null)));
// }
Createmode.ephemeral temporary nodes can be used for distributed locks
String ret = zk.create ("/testroot/children", "Children data". GetBytes (), Ids.open_acl_unsafe, createmode.ephemeral) ;
System.out.println ("======" +ret);
Thread.Sleep (10*1000);
Create child nodes
Modify the value of a node
Zk.setdata ("/testroot", "Modify data Root". GetBytes (),-1);
byte[] data = Zk.getdata ("/testroot", false, NULL);
System.out.println (new String (data));
Determine if a node exists
System.out.println (zk.exists ("/testroot/a1", false));
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.