zoukankan      html  css  js  c++  java
  • Zookeeper中Watcher监听实现增删改

    8.1 连接方法

    package com.zookeeper.day01;
    
    import org.apache.zookeeper.*;
    
    import java.io.IOException;
    
    public class ZookeeperWatcher implements Watcher {
        //连接地址
        private static final String ZK_ADDRESS="0.0.0.0:2181";
        //超时时间
        private static final Integer ZK_TIMEOUT=2000;
    
        private ZooKeeper zooKeeper;
    
        public ZookeeperWatcher() {
            openConnection(ZK_ADDRESS,ZK_TIMEOUT);
    
        }
    
        /**
         * 回调方法,监听连接,监听增删改节点
         * @param watchedEvent
         */
        @Override
        public void process(WatchedEvent watchedEvent) {
            //获取当前的状态
            Event.KeeperState keeperState = watchedEvent.getState();
            //获取通知类型
            Event.EventType eventType=watchedEvent.getType();
            //获取操作节点的路径
            String path=watchedEvent.getPath();
    
            System.out.println("当前状态为:"+keeperState+"	通知类型为:"+eventType+"	操作的节点路径:"+path);
    
            //已经成功连接
            if (Event.KeeperState.SyncConnected==keeperState){
                //连接状态
                if (Event.EventType.None==eventType){
                    System.out.println("--------连接事件回调--------");
                }
            }
        }
    
        /**
         * 连接方法
         */
        public void openConnection(String zk_address,Integer zk_timeout){
            try {
                zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
                System.out.println("Zookeeper连接成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭连接
         */
        public void closeConnection(){
            if (zooKeeper!=null){
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
     
    
        public static void main(String[] args) {
            //创建实体类对象
            ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
            //关闭连接
            zookeeperWatcher.closeConnection();
        }
    }
    View Code 

    控制台效果:

      

    8.2 添加节点

    package com.zookeeper.day01;
    
    import org.apache.zookeeper.*;
    
    import java.io.IOException;
    
    public class ZookeeperWatcher implements Watcher {
        //连接地址
        private static final String ZK_ADDRESS="0.0.0.0:2181";
        //超时时间
        private static final Integer ZK_TIMEOUT=2000;
    
        private ZooKeeper zooKeeper;
    
        public ZookeeperWatcher() {
            openConnection(ZK_ADDRESS,ZK_TIMEOUT);
    
        }
    
        /**
         * 回调方法,监听连接,监听增删改节点
         * @param watchedEvent
         */
        @Override
        public void process(WatchedEvent watchedEvent) {
            //获取当前的状态
            Event.KeeperState keeperState = watchedEvent.getState();
            //获取通知类型
            Event.EventType eventType=watchedEvent.getType();
            //获取操作节点的路径
            String path=watchedEvent.getPath();
    
            System.out.println("当前状态为:"+keeperState+"	通知类型为:"+eventType+"	操作的节点路径:"+path);
    
            //已经成功连接
            if (Event.KeeperState.SyncConnected==keeperState){
                //连接状态
                if (Event.EventType.None==eventType){
                    System.out.println("--------连接事件回调--------");
                }
                //创建节点
                if (Event.EventType.NodeCreated==eventType){
                    System.out.println("------创建节点事件回调------");
                }
            }
        }
    
        /**
         * 连接方法
         */
        public void openConnection(String zk_address,Integer zk_timeout){
            try {
                zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
                System.out.println("Zookeeper连接成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭连接
         */
        public void closeConnection(){
            if (zooKeeper!=null){
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 添加节点
         */
        public void createNode(String path,String data){
            try {
                //启动监听
                zooKeeper.exists(path,true);
                String result = zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                System.out.println("创建节点成功:"+result);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
     
    
        public static void main(String[] args) {
            //创建实体类对象
            ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
            //调用创建节点方法
            zookeeperWatcher.createNode("/zkNode","zkValue");
            //关闭连接
            zookeeperWatcher.closeConnection();
        }
    }
    View Code

    控制台效果:

       

    8.3 修改节点

    package com.zookeeper.day01;
    
    import org.apache.zookeeper.*;
    
    import java.io.IOException;
    
    public class ZookeeperWatcher implements Watcher {
        //连接地址
        private static final String ZK_ADDRESS="0.0.0.0:2181";
        //超时时间
        private static final Integer ZK_TIMEOUT=2000;
    
        private ZooKeeper zooKeeper;
    
        public ZookeeperWatcher() {
            openConnection(ZK_ADDRESS,ZK_TIMEOUT);
    
        }
    
        /**
         * 回调方法,监听连接,监听增删改节点
         * @param watchedEvent
         */
        @Override
        public void process(WatchedEvent watchedEvent) {
            //获取当前的状态
            Event.KeeperState keeperState = watchedEvent.getState();
            //获取通知类型
            Event.EventType eventType=watchedEvent.getType();
            //获取操作节点的路径
            String path=watchedEvent.getPath();
    
            System.out.println("当前状态为:"+keeperState+"	通知类型为:"+eventType+"	操作的节点路径:"+path);
    
            //已经成功连接
            if (Event.KeeperState.SyncConnected==keeperState){
                //连接状态
                if (Event.EventType.None==eventType){
                    System.out.println("--------连接事件回调--------");
                }
                //修改节点
                if(Event.EventType.NodeDataChanged==eventType){
                    System.out.println("------修改节点事件回调------");
                }
    
        /**
         * 连接方法
         */
        public void openConnection(String zk_address,Integer zk_timeout){
            try {
                zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
                System.out.println("Zookeeper连接成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭连接
         */
        public void closeConnection(){
            if (zooKeeper!=null){
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 修改节点
         */
        public void setNode(String path,String data){
            try {
                //启动监听
                zooKeeper.exists(path,true);
                zooKeeper.setData(path, data.getBytes(), -1);
                System.out.println("修改节点成功");
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            //创建实体类对象
            ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
            //调用修改节点方法
            zookeeperWatcher.setNode("/zkNode","zkValueSet");
            //关闭连接
            zookeeperWatcher.closeConnection();
        }
    }
    View Code 

    控制台效果:

       

    8.4 删除节点

    package com.zookeeper.day01;
    
    import org.apache.zookeeper.*;
    
    import java.io.IOException;
    
    public class ZookeeperWatcher implements Watcher {
        //连接地址
        private static final String ZK_ADDRESS="0.0.0.0:2181";
        //超时时间
        private static final Integer ZK_TIMEOUT=2000;
    
        private ZooKeeper zooKeeper;
    
        public ZookeeperWatcher() {
            openConnection(ZK_ADDRESS,ZK_TIMEOUT);
    
        }
    
        /**
         * 回调方法,监听连接,监听增删改节点
         * @param watchedEvent
         */
        @Override
        public void process(WatchedEvent watchedEvent) {
            //获取当前的状态
            Event.KeeperState keeperState = watchedEvent.getState();
            //获取通知类型
            Event.EventType eventType=watchedEvent.getType();
            //获取操作节点的路径
            String path=watchedEvent.getPath();
    
            System.out.println("当前状态为:"+keeperState+"	通知类型为:"+eventType+"	操作的节点路径:"+path);
    
            //已经成功连接
            if (Event.KeeperState.SyncConnected==keeperState){
                //连接状态
                if (Event.EventType.None==eventType){
                    System.out.println("--------连接事件回调--------");
                }
                //删除节点
                if(Event.EventType.NodeDeleted==eventType){
                    System.out.println("------删除节点事件回调------");
                }
            }
        }
    
        /**
         * 连接方法
         */
        public void openConnection(String zk_address,Integer zk_timeout){
            try {
                zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
                System.out.println("Zookeeper连接成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 关闭连接
         */
        public void closeConnection(){
            if (zooKeeper!=null){
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
     
        /**
         * 删除节点
         */
        public void deleteNode(String path){
            try {
                //启动监听
                zooKeeper.exists(path,true);
                zooKeeper.delete(path, -1);
                System.out.println("删除节点成功");
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            //创建实体类对象
            ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
            //调用删除节点方法
            zookeeperWatcher.deleteNode("/zkNode");
            //关闭连接
            zookeeperWatcher.closeConnection();
        }
    }
    View Code

    控制台效果:

      

  • 相关阅读:
    Linux下静态库与动态库
    通过js操作样式(评分)
    javascript学习
    2017年6月1日学习
    javascript学习2
    javascript学习:闭包和prototype原型使用基础
    关于“System.Data.ProviderIncompatibleException”类型的异常
    Android性能优化之ViewStub
    Activity Threa创建Window和View分析
    软键盘触发后弹起底部布局文件方法
  • 原文地址:https://www.cnblogs.com/Zzzzn/p/11934633.html
Copyright © 2011-2022 走看看