真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

zookeeperJavaapi

maven依賴:

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于網站設計、成都網站建設、西寧網絡推廣、重慶小程序開發(fā)、西寧網絡營銷、西寧企業(yè)策劃、西寧品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供西寧建站搭建服務,24小時服務熱線:13518219792,官方網址:www.cdcxhl.com



  4.0.0
  per.ym
  zk
  0.0.1-SNAPSHOT

  
    
      org.apache.zookeeper
      zookeeper
      3.4.12
    

    
        junit
        junit
        4.12
        test
    
  

測試類:

package per.ym.zookeeper;

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

import org.apache.zookeeper.AsyncCallback.ChildrenCallback;
import org.apache.zookeeper.AsyncCallback.DataCallback;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.AsyncCallback.StringCallback;
import org.apache.zookeeper.AsyncCallback.VoidCallback;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ZkTest implements Watcher{

    private ZooKeeper zk;

    private CountDownLatch cdl;

    private String path = "/test";

    private String rootPath = "/";

    private int sessionTimeOut = 15000;

    private byte[] data = "data".getBytes();

    private byte[] newData = "newData".getBytes();

    @Before
    public void connect() throws IOException {
        zk = new ZooKeeper("192.168.61.131:2184", sessionTimeOut, this);
        cdl = new CountDownLatch(1);
    }

    //同步調用
    @Test
    public void testSync( ) throws Exception {
        //等待與zookeeper服務端連接完成
        cdl.await();

        //創(chuàng)建一個持久節(jié)點/test,并為其賦值為data
        zk.create("/test", data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        //獲取根節(jié)點下的子節(jié)點
        List children = zk.getChildren("/", this);
        System.out.println("根節(jié)點下的子節(jié)點有: " + children);

        //查詢節(jié)點/test保存的數據
        Stat stat = new Stat();
        byte[] tempData = zk.getData(path, this, stat);
        System.out.println("test節(jié)點數據為: " + new String(tempData));

        //設置節(jié)點/test的數據為newData
        zk.setData(path, newData, -1);
        tempData = zk.getData(path, this, stat);
        System.out.println("test節(jié)點新數據為: " + new String(tempData));

        //刪除節(jié)點/test
        zk.delete(path, -1);

        //判斷節(jié)點/test是否存在
        stat = zk.exists(path, this);
        System.out.println(stat);
    }

    //創(chuàng)建節(jié)點后進行回調
    private StringCallback cb = new StringCallback() {

        @Override
        public void proce***esult(int rc, String path, Object ctx, String name) {
            switch (Code.get(rc)) { 
                case CONNECTIONLOSS:
                    //如果失去連接,我們無法保證是在創(chuàng)建前還是創(chuàng)建后丟失的,因此重試
                    create();
                    break;
                case OK:
                    System.out.println("節(jié)點/test創(chuàng)建成功");
                    break;
                case NODEEXISTS:
                    System.out.println("節(jié)點/test已經存在");
                    break;
                default:
                    System.out.println(KeeperException.create(Code.get(rc), path));
                }

        }
    };

    //獲取子節(jié)點進行回調
    private ChildrenCallback ccb = new ChildrenCallback() {

        @Override
        public void proce***esult(int rc, String path, Object ctx, List children) {
            switch (Code.get(rc)) {
                case CONNECTIONLOSS:
                    getChildren();
                    break;
                case OK:
                    System.out.println("根節(jié)點下的子節(jié)點有: " + children);
                    break;
                default:
                    System.out.println(KeeperException.create(Code.get(rc), path));
            }
        }

    };

    //獲取數據進行回調
    private DataCallback dcb = new DataCallback() {

        @Override
        public void proce***esult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            switch (Code.get(rc)) {
                case CONNECTIONLOSS:
                    getData();
                    break;
                case OK:
                    System.out.println("test節(jié)點數據為: " + new String(data));
                    break;
                default:
                    System.out.println(KeeperException.create(Code.get(rc), path));
            }
        }

    };

    //設置數據進行回調
    private StatCallback scb = new StatCallback() {

        @Override
        public void proce***esult(int rc, String path, Object ctx, Stat stat) {
            switch (Code.get(rc)) {
                case CONNECTIONLOSS:
                    setData();
                    break;
                case OK:
                    //這個ctx就是我們調用zk.setData時傳入的最后一個參數
                    System.out.println("test節(jié)點設置新數 " + new String((byte[])ctx) + "成功");
                    break;
                default:
                    System.out.println(KeeperException.create(Code.get(rc), path));
            }
        }

    };

    //刪除節(jié)點進行回調
    private VoidCallback vcb = new VoidCallback() {

        @Override
        public void proce***esult(int rc, String path, Object ctx) {
            switch (Code.get(rc)) {
                case CONNECTIONLOSS:
                    delete();
                    break;
                case OK:
                    System.out.println("刪除節(jié)點/test成功");
                    break;
                default:
                    System.out.println(KeeperException.create(Code.get(rc), path));
            }
        }

    };

    //節(jié)點是否存在進行回調
    private StatCallback scb2 = new StatCallback() {

        @Override
        public void proce***esult(int rc, String path, Object ctx, Stat stat) {
            switch (Code.get(rc)) { 
                case CONNECTIONLOSS:
                    exists();
                    break;
                case OK:
                    System.out.println("節(jié)點/test存在");
                    break;
                case NONODE:
                    System.out.println("節(jié)點/test不存在");
                    break;
                default:
                    break;
            }
        }

    };

    //異步調用
    @Test
    public void testAsync() throws Exception {
        cdl.await();

        create();

        getChildren();

        setData();

        getData();

        exists();

        delete();

        exists();
    }

    private void create() {
        zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, cb, data);
    }

    private void getChildren() {
        zk.getChildren(rootPath, this, ccb, null);
    }

    private void getData() {
        zk.getData(path, this, dcb, null);
    }

    private void setData() {
        zk.setData(path, newData, -1, scb, newData);
    }

    private void delete() {
        zk.delete(path, -1, vcb, null);
    }

    private void exists() {
        zk.exists(path, this, scb2, null);
    }

    @Override
    public void process(WatchedEvent event) {
        //如果連接成功,放行
        if (event.getType().equals(EventType.None)) {
            cdl.countDown();
        }

        System.out.println(event);
    }

    @After
    public void close() throws InterruptedException {
        //測試完成后關閉連接
        zk.close();
    }
}

分享文章:zookeeperJavaapi
標題網址:http://weahome.cn/article/iedjhg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部