zoukankan      html  css  js  c++  java
  • zookeeper

    package org.huqi.demo;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.KeeperException;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    import org.apache.zookeeper.ZooKeeper;
    import org.apache.zookeeper.data.Stat;
    import org.apache.zookeeper.ZooDefs.Ids;
    
    public class Demo {
        private static ZooKeeper zookeeper=null;
        public static void main(String[] args) throws Exception {
            
                zookeeper=new ZooKeeper("localhost:2181", 2000, new Watcher() {
                    
                    @Override
                    public void process(WatchedEvent event) {
                        try {
                            getNode();
                        } catch (KeeperException | InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                });
                //deleteNode();
                //createNode();
                //isExtistNode();
                
                //getNode();
                
                Thread.sleep(Long.MAX_VALUE);
        }
        /***
         * 获取节点的权限
         * @throws InterruptedException 
         * @throws KeeperException 
         * 
         * */
        public static void getNode() throws KeeperException, InterruptedException {
            //path 节点路径
            //watch 是否监听
            List<String> nodes=zookeeper.getChildren("/", true);
            System.out.println("----------------------------");
            for(String ns:nodes) {
                System.out.println(ns);
            }
            System.out.println("----------------------------");
            
        }
        /**
         * 创建节点
         * @throws InterruptedException 
         * @throws KeeperException 
         * */
        public static void createNode() throws KeeperException, InterruptedException {
        /**
         * Ids.OPEN_ACL_UNSAFE 开放的节点
         * CREATOR_ALL_ACL 可以创建的权限
         * READ_ACL_UNSAFE  只读的权限
         * CreateMode.PERSISTENT_SEQUENTIAL 带序号的不担心节点重名
         * EPHEMERAL 短暂的节点
         * PERSISTENT 永久的 节点
         * */
        String path=zookeeper.create("/huqi2000", "huqi11".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(path);
        }
        /**
         * 删除节点
         * */
        public static void deleteNode() throws InterruptedException, KeeperException {
            zookeeper.delete("/huqi211", 0);
        }
        /**
         * 判断节点是否存在,并且不监听
         * @throws InterruptedException 
         * @throws KeeperException 
         * */
        public static void isExtistNode() throws KeeperException, InterruptedException {
            Stat path=zookeeper.exists("/huqi211", false);
            System.out.println(path);
            System.out.println(path.getAversion());
            System.out.println(path.getCtime());
        }
    }
  • 相关阅读:
    Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
    快速排序及三向切分快排——java实现
    自顶向下(递归)的归并排序和自底向上(循环)的归并排序——java实现
    希尔shell排序——java实现
    插入排序——java实现
    选择排序——java实现
    Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list
    单向链表的归并排序——java实现
    Coursera Algorithms week3 归并排序 练习测验: Counting inversions
    Coursera Algorithms week2 栈和队列 练习测验: Stack with max
  • 原文地址:https://www.cnblogs.com/huqi96/p/13585131.html
Copyright © 2011-2022 走看看