Java Learning Note (iv) Java connection zookeeper

Source: Internet
Author: User

public class Demo2 {

public static void main(String[] args) {    String connectString = "192.168.168.133:2181";    int sessionTimeout = 500000;  // 会话时间。设置长一点,如果不够长,则会connect loss    try {        // 建立zookeeper链接        ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, new Watcher(){            public void process(WatchedEvent event) {                // 监控所有触发的事件                System.out.println("start do something");                EventType type = event.getType();        // 事件类型。枚举                KeeperState state = event.getState();    // 状态。 枚举

//Type.getintvalue ();
Switch (type) {
Case None://INT value corresponds to-1
System.out.println ("None event triggered");
Break
Case nodecreated://INT value corresponds to 1
System.out.println ("Create node event occurred");
Break
Case nodedeleted://INT value corresponds to 2
System.out.println ("Delete node event occurred");
Break
Case nodedatachanged://INT value corresponds to 3
System.out.println ("Node data Change event has occurred");
Break
Case nodechildrenchanged://INT value corresponds to 4
System.out.println ("Child node Change event occurred");
Break
Default:
System.out.println ("I don't know what operate");
Break
}
//State.getintvalue (); nosyncconnected 1 and Unknown-1
Switch (state) {
Case disconnected://0
System.out.println ("Lost Connection");
Break
Case syncconnected://3
System.out.println ("async link");
Break
Case Expired:// -112
System.out.println ("timeout expires");
Break
Default:
break;
}

                System.out.println ("End");        }        });        System.out.println ("ZK creation Success"); list<string> list = Zk.getchildren ("/", true);        Gets all nodes if (list! = null) {for (String s:list) System.out.println (s); }//Create child node Zk.create ("/app7", "/APP8 sub-node data" under the/APP5 node. GetBytes (), Ids.open_acl_unsafe, createmode.persist ENT); Will persist for the data, the next start of the service is Zk.create ("/app8", "/APP8 Child Data"). GetBytes (), Ids.open_acl_unsafe, Createmode.                Persistent, new Stringcallback () {public void processresult (int rc, string path, Object ctx, string name) {                SYSTEM.OUT.PRINTLN ("RC:" + RC);                System.out.println ("Path:" + path);                System.out.println ("Object:" + ctx);            System.out.println ("Name:" + name);    }}, "CTx object");    } catch (IOException e) {e.printstacktrace (); } catch (Keeperexception e) {E.printstacktraCE ();    } catch (Interruptedexception e) {e.printstacktrace (); }}

}

The ZK will be monitored and watcher recorded every time it is manipulated.

This error occurs because the parent node was not created when the node was created/APP1

Java Learning Note (iv) Java connection 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.