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());
        }
    }
  • 相关阅读:
    常用知识点集合
    LeetCode 66 Plus One
    LeetCode 88 Merge Sorted Array
    LeetCode 27 Remove Element
    LeetCode 26 Remove Duplicates from Sorted Array
    LeetCode 448 Find All Numbers Disappeared in an Array
    LeetCode 219 Contains Duplicate II
    LeetCode 118 Pascal's Triangle
    LeetCode 119 Pascal's Triangle II
    LeetCode 1 Two Sum
  • 原文地址:https://www.cnblogs.com/huqi96/p/13585131.html
Copyright © 2011-2022 走看看