Zookeeper node additions and deletions to check the small demo

Source: Internet
Author: User
Tags sleep zookeeper
public class Zookeeperbase {


/*****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 ();

System.out.println ("carried out");
//
Thread.Sleep (5000);

Create parent Node
String ret = zk.create ("/testroot", "/testroot". GetBytes (), Ids.open_acl_unsafe, createmode.persistent);
System.out.println ("======" +ret);

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

Delete a node
Zk.delete ("/testroot", -1,new Asynccallback.voidcallback () {
//
@Override
public void Processresult (int r0, String path, Object ctx) {
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
// }
System.out.println (R0);
SYSTEM.OUT.PRINTLN (path);
System.out.println (CTX);
// }
}, "a");
System.out.println ("========== continue to execute");

Thread.Sleep (10000);
Zk.close ();

}
}

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.