Working with Java Zookeeper
package cn.sniper.zookeeper;import java.io.ioexception;import java.util.list;import Java.util.treeset;import org.apache.zookeeper.createmode;import org.apache.zookeeper.keeperexception ;import org.apache.zookeeper.watchedevent;import org.apache.zookeeper.watcher;import Org.apache.zookeeper.zoodefs;import org.apache.zookeeper.zookeeper;import org.junit.test;public class ZookeeperUtil { @Test public void helloword () { //Port is 2181 //string connectstring = "192.168.1.231" by default; string connectstring = "192.168.1.231:2181"; int sessiontimeout = 20000; try { zookeeper zk = new zookeeper (ConnectString, sessiontimeout, new watcher () { public void process ( Watchedevent event) { &Nbsp; system.out.println (event); } }); system.out.println (ZK); zk.close (); } catch ( ioexception e) { e.printstacktrace (); } catch ( interruptedexception e) { e.printstacktrace (); } } @Test public void create () { String connectString = " 192.168.1.231:2181 "; int sessiontimeout = 20000; try { zookeeper zk = new zookeeper (connectstring, sessiontimeout, New watcher () { public void process (WatchedEvent event) { system.err.println ("Event Type:" + event.gettype ()); } }); //Create Node zk.create ("/sniper1", "". GetBytes (), zoodefs.ids.open_acl_unsafe, createmode.persistent); zk.close (); } catch ( ioexception e) { e.printstacktrace (); } catch ( interruptedexception e) { e.printstacktrace (); } catch ( keeperexception e) { e.printstacktrace (); } } @Test public void fifoin () { String connectString = " 192.168.1.231,192.168.1.232,192.168.1.233 "; int sessiontimeout = 20000; try { zookeeper zk = new zookeeper (connectString, Sessiontimeout, new watcher () { public void process ( Watchedevent event) { system.err. println ("Event Type:" + event.gettype ()); } }); //each client is connected in the FIFO to create an ordered node simulate 10 client connections into for (int i=0; i<10; i++) { zk.create ("/fifo/", string.valueof ( System.currenttimemillis ()). GetBytes (), zoodefs.ids.open_acl_unsafe, createmode.persistent_ sequential); } zk.close (); } catch ( ioexception e) { e.printstacktrace (); } catch ( interruptedexception e) { e.printstacktrace (); } catch ( keeperexception e) { e.printstacktrace (); } } @Test public void fifoout () { String connectString = " 192.168.1.231,192.168.1.232,192.168.1.233 "; int sessiontimeout = 30000; try { zookeeper zk = new ZooKeeper (Connectstring, sessiontimeout, new watcher () { public void process (watchedevent event) { system.err.println ("Event type: " + event.gettype ()); } }); list<string> children = zk.getchildren ("/fifo", new watcher () { public void process (watchedevent event) { system.err.println ("Event Type:" + event.gettype ()); } }); //because of the order of the node, the node is sorted by TreeSet, the first element can be achieved in the FIFO queue treeset <String> set = new TreeSet<String> (children); string child =&nBsp;set.first (); system.err.println (child); zk.delete ("/fifo/" +child, -1); zk.close (); } catch (ioexception e) { e.printstacktrace (); } catch ( interruptedexception e) { e.printstacktrace (); } catch ( keeperexception e) { e.printstacktrace (); } } }
Working with Java Zookeeper