请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

为什么process方法获取到的事件类型都是None,附代码

package com.zookeeper.crud;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
* @author KingWarLuo(jianhua.luo@ucarinc.com)
* @description: ${description}
* @date 2018-08-15 17:26
*/
public class ZkCRUD implements Watcher {

   private ZooKeeper zookeeper;
   private static Stat stat = new Stat();

   public static String SERVER_IP_PORT = "10.112.11.101:3001";
   public static int timeout = 5000;

   public static CountDownLatch countDown = new CountDownLatch(1);

   public ZkCRUD(){
       try{
           zookeeper =  new ZooKeeper(SERVER_IP_PORT, timeout, new ZkCRUD());
       } catch(Exception e){
           e.printStackTrace();
           if(zookeeper != null){
               try {
                   zookeeper.close();
               } catch (InterruptedException e1) {
                   e1.printStackTrace();
               }
           }
       }
   }

   public static void main(String[] args) {
       try {
           research();
//            create();
//            update();
//            delete();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   public static void research() throws Exception {
       ZkCRUD zk = new ZkCRUD();
       byte[] bytes = zk.getZookeeper().getData("/kingwarluo", true, stat);
       String result = new String(bytes);
       System.out.println(result);
       countDown.countDown();
   }

   public static void create() throws Exception {
       String ctx = "{create:success}";
       //同步
//        new ZkCRUD().getZookeeper().create("/kingwarluo", "kingwarluo".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
       //异步callback
       new ZkCRUD().getZookeeper().create("/kingwarluo2", "kingwarluo".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new CreateCallBack(), ctx);
       Thread.sleep(2000);
   }

   public static void update() throws IOException, KeeperException, InterruptedException {
       String ctx = "{create:success}";
       //同步
       new ZkCRUD().getZookeeper().setData("/kingwarluo2", "luojianhua".getBytes(), 2);
       //异步callback
//        new ZkCRUD().getZookeeper().setData("/kingwarluo2", "luojianhua".getBytes(), 1, new UpdateCallBack(), ctx);
//        Thread.sleep(2000);
   }

   public static void delete() throws IOException, KeeperException, InterruptedException {
       String ctx = "{create:success}";
       //同步
       new ZkCRUD().getZookeeper().delete("/kingwarluo", 2);
       //异步callback
//        new ZkCRUD().getZookeeper().delete("/kingwarluo2", 1, new DeleteCallBack(), ctx);
//        Thread.sleep(2000);
   }

   @Override
   public void process(WatchedEvent event) {
       System.out.println("========================="+ event.getType()+"=======================");
       try{
           if(Watcher.Event.EventType.NodeDataChanged.equals(event.getType())){
               ZkCRUD zk = new ZkCRUD();
               byte[] bytes = zk.getZookeeper().getData("/kingwarluo2", false, null);
               System.out.println("修改后的值:" + new String(bytes));
               countDown.countDown();
           }else if(Watcher.Event.EventType.NodeCreated.equals(event.getType())){

           }else if(Watcher.Event.EventType.NodeDeleted.equals(event.getType())){

           }else if(Watcher.Event.EventType.NodeChildrenChanged.equals(event.getType())){

           }
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           System.out.println(event.getType() + "事件执行完成。");
       }
   }

   public ZooKeeper getZookeeper() {
       return zookeeper;
   }

   public void setZookeeper(ZooKeeper zookeeper) {
       this.zookeeper = zookeeper;
   }
}

正在回答 回答被采纳积分+3

2回答

风间影月 2018-09-12 11:21:28

https://img1.sycdn.imooc.com//szimg/5b9886360001a7d810011001.jpg
这可读性很差啊…直接把代码贴上来看不出啥,说清楚你的情况,比如报错信息啊什么的。或者你直接到群里交流也可以

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_RockNRoll #1
    用ueEditor预览正常,点保存格式就乱了,这是不是bug。。
    回复 有任何疑惑可以回复我~ 2018-09-12 11:23:30
  • 风间影月 回复 提问者 qq_RockNRoll #2
    这我就不知道啦,要不你到群里发了问问看
    回复 有任何疑惑可以回复我~ 2018-09-12 11:26:26
风间影月 2018-09-12 10:42:14

什么叫获取的时间为none?可以详细一点吗

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_RockNRoll #1
    已修改,就是process里面事件类型,获取到都是none
    回复 有任何疑惑可以回复我~ 2018-09-12 11:21:48
  • 风间影月 回复 提问者 qq_RockNRoll #2
    打断点看看吧,代码风格无法阅读啊……手机上看不了啊,哈哈,你贴到群里去
    回复 有任何疑惑可以回复我~ 2018-09-12 11:23:06
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信