java API to operate zookeeper

package com.irisian.zookeeper;

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

import org.apache.zookeeper.AsyncCallback;
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.Watcher.Event.KeeperState;
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 ZookeeperTest {
    private ZooKeeper zk = null;
    /** zookeeper address*/
    private String CONNECT_ADDR = "irisian01:2181,irisian01:2181,irisian01:2181";
    /**session timeout*/
    private int SESSION_OUTTIME = 2000;/ / ms
    /** Semaphore, blocking program execution, used to wait for zookeeper to connect successfully and send a successful signal */
    private CountDownLatch connectedSemaphore = new CountDownLatch(1);

    @Before
    public void init() throws IOException, InterruptedException {
        zk = new ZooKeeper (CONNECT_ADDR, SESSION_OUTTIME, new Watcher() {
            public void process(WatchedEvent event) {
                // Get the state of the event
                KeeperState keeperState = event.getState();
                EventType eventType = event.getType();
                // If the connection is established
                if (KeeperState.SyncConnected == keeperState) {
                    if (EventType.None == eventType) {
                        // If the connection is established successfully, send a semaphore to block subsequent blocks The program executes down
                        connectedSemaphore.countDown();
                        System.out.println("zk establishes a connection");
                    }
                }
            }
        });
        // block
        connectedSemaphore.await();
    }

    @Test
    public void add() throws KeeperException, InterruptedException {
        zk.create("/name", "wumanxin".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    @Test
    public void get() throws Exception {
        byte[] data = zk.getData("/name", false, null);
        System.out.println(new String(data));
    }

    @Test
    public void mod() throws KeeperException, InterruptedException {
        zk.setData("/name", "wumanxin2018".getBytes(), -1);
    }

    // 判断节点是否存在
    @Test
    public void isExist() throws KeeperException, InterruptedException {
        Stat exists = zk.exists("/name", false);
        if (exists != null) {
            System.out.println("name exists");
        } else {
            System.out.println("name does not exist");
        }
    }

    // Synchronous deletion
    @Test
    public void del() throws InterruptedException, KeeperException {
        zk.delete( "/name", -1);
    }

    // delete asynchronously
    @Test
    public void asyDel() {
        // delete node asynchronously
        zk.delete("/name", -1, new AsyncCallback.VoidCallback() {
            public void processResult( int rc, String path, Object ctx) {
                System.out.println("rc=====" + rc);
                System.out.println("path======" + path);
                System.out.println("ctc======" + path);
            }
        }, "回调值");

    }

    @After
    public void close() throws InterruptedException {
        zk.close();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597761&siteId=291194637