• 通知机制程序代码_java培训


    1.程序代码

    • 一次性

    package com.atguigu.zk3;

    import java.io.IOException;

    import org.apache.log4j.Logger;

    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.ZooDefs.Ids;

    import org.apache.zookeeper.data.Stat;

    public class WatchOne

    {

       /**

       * Logger for this class

       */

       private static final Logger logger = Logger.getLogger(HelloZK.class);

       //定义常量

       private static final String CONNECTSTRING = “192.168.67.167:2181”;

       private static final String PATH = “/atguigu”;

       private static final int    SESSION_TIMEOUT = 50*1000;

       //定义实例变量

       private ZooKeeper zk = null;

       //以下为业务方法

       public ZooKeeper startZK() throws IOException

       {

           return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {

              @Override

              public void process(WatchedEvent event)

              {

              }

           });

       }

       public void stopZK() throws InterruptedException

       {

           if(zk != null)

           {

              zk.close();

           }

       }

       public void createZNode(String path,String nodeValue) throws KeeperException, InterruptedException

       {

           zk.create(path,nodeValue.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

       }

       public String getZNode(String path) throws KeeperException, InterruptedException

       {

           byte[] byteArray = zk.getData(path,new Watcher() {

              @Override

              public void process(WatchedEvent event)

              {

                  try

                  {

                     triggerValue(path);

                  }catch (KeeperException | InterruptedException e) {

                     e.printStackTrace();

                  }

              }

           }, new Stat());

           return new String(byteArray);

       }

       public String triggerValue(String path) throws KeeperException, InterruptedException

       {

           byte[] byteArray = zk.getData(path,false, new Stat());

           String retValue = new String(byteArray);

           System.out.println(“**************triggerValue: “+retValue);

           return retValue;

       }

       public static void main(String[] args) throws IOException, KeeperException, InterruptedException

       {

           WatchOne watchOne = new WatchOne();

           watchOne.setZk(watchOne.startZK());

           if(watchOne.getZk().exists(PATH, false) == null)

           {

              watchOne.createZNode(PATH,”BBB”);

              System.out.println(“**********************>: “+watchOne.getZNode(PATH));

              Thread.sleep(Long.MAX_VALUE);

           }else{

              System.out.println(“i have znode”);

           }

       }

       //setter—getter

       public ZooKeeper getZk()

       {

           return zk;

       }

       public void setZk(ZooKeeper zk)

       {

           this.zk = zk;

       } 

    }

    • 多次

    package com.atguigu.zk3;

    import java.io.IOException;

    import org.apache.log4j.Logger;

    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.ZooDefs.Ids;

    import org.apache.zookeeper.data.Stat;

    public class WatchMoreTest

    {

       /**

       * Logger for this class

       */

       private static final Logger logger = Logger.getLogger(WatchMoreTest.class);

       //定义常量

       private static final String CONNECTSTRING = “192.168.67.167:2181”;

       private static final String PATH = “/atguigu”;

       private static final int    SESSION_TIMEOUT = 50*1000;

       //定义实例变量

       private ZooKeeper zk = null;

       private String lastValue = “”;

       //以下为业务方法

       public ZooKeeper startZK() throws IOException

       {

           return new ZooKeeper(CONNECTSTRING, SESSION_TIMEOUT, new Watcher() {

              @Override

              public void process(WatchedEvent event)

              {}

           });

       }

       public void stopZK() throws InterruptedException

       {

           if(zk != null)

           {

              zk.close();

           }

       }

       public void createZNode(String path,String nodeValue) throws KeeperException, InterruptedException

       {

           zk.create(path,nodeValue.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

       }

       public String getZNode(String path) throws KeeperException, InterruptedException

       {

           byte[] byteArray = zk.getData(path,new Watcher() {

              @Override

              public void process(WatchedEvent event)

              {

                  try

                  {

                     triggerValue(path);

                  }catch (KeeperException | InterruptedException e) {

                     e.printStackTrace();

                  }

              }

           }, new Stat());

           return new String(byteArray);

       }

       public boolean triggerValue(String path) throws KeeperException, InterruptedException

       {

           byte[] byteArray = zk.getData(path,new Watcher() {

              @Override

              public void process(WatchedEvent event)

              {

                  try

                  {

                     triggerValue(path);

                  }catch (KeeperException | InterruptedException e) {

                     e.printStackTrace();

                  }

              }

           }, new Stat());

           String newValue = new String(byteArray);

           if(lastValue.equals(newValue))

           {

              System.out.println(“there is no change~~~~~~~~”);

              return false;

           }else{

              System.out.println(“lastValue: “+lastValue+”\t”+”newValue: “+newValue);

              this.lastValue = newValue;

              return true;

           }

       }

       public static void main(String[] args) throws IOException, KeeperException, InterruptedException

       {

           WatchMoreTest watch = new WatchMoreTest();

           watch.setZk(watch.startZK());

           if(watch.getZk().exists(PATH, false) == null)

           {

              String initValue = “0000”;

              watch.setLastValue(initValue);

              watch.createZNode(PATH,initValue);

              System.out.println(“**********************>: “+watch.getZNode(PATH));

              Thread.sleep(Long.MAX_VALUE);

           }else{

              System.out.println(“i have znode”);

           }

       }

       //setter—getter

       public ZooKeeper getZk()

       {

           return zk;

       }

       public void setZk(ZooKeeper zk)

       {

           this.zk = zk;

       }

       public String getLastValue()

       {

           return lastValue;

       }

       public void setLastValue(String lastValue)

       {

           this.lastValue = lastValue;

       } 

    }

  • 相关阅读:
    Java面试题以及答案(一)Java基础知识
    springboot 获取resource文件下的图片路径
    祥云杯2022 pwn - protocol
    QPlainTextEdit
    Kafka高性能高吞吐的原因总结
    【面试题】sychronized
    kafka学习-基本概念与简单实战
    STM32G030F6P6TR 芯片TSSOP20 MCU单片机微控制器芯片
    Java面试题总结(二)
    Error message “error:0308010C:digital envelope routines::unsupported“
  • 原文地址:https://blog.csdn.net/zjjcchina/article/details/128198809