Working with Java Zookeeper

Source: Internet
Author: User

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

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.