maven依賴:
岢嵐網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
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);
}
//同步調(diào)用
@Test
public void testSync( ) throws Exception {
//等待與zookeeper服務(wù)端連接完成
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保存的數(shù)據(jù)
Stat stat = new Stat();
byte[] tempData = zk.getData(path, this, stat);
System.out.println("test節(jié)點數(shù)據(jù)為: " + new String(tempData));
//設(shè)置節(jié)點/test的數(shù)據(jù)為newData
zk.setData(path, newData, -1);
tempData = zk.getData(path, this, stat);
System.out.println("test節(jié)點新數(shù)據(jù)為: " + new String(tempData));
//刪除節(jié)點/test
zk.delete(path, -1);
//判斷節(jié)點/test是否存在
stat = zk.exists(path, this);
System.out.println(stat);
}
//創(chuàng)建節(jié)點后進行回調(diào)
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已經(jīng)存在");
break;
default:
System.out.println(KeeperException.create(Code.get(rc), path));
}
}
};
//獲取子節(jié)點進行回調(diào)
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));
}
}
};
//獲取數(shù)據(jù)進行回調(diào)
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é)點數(shù)據(jù)為: " + new String(data));
break;
default:
System.out.println(KeeperException.create(Code.get(rc), path));
}
}
};
//設(shè)置數(shù)據(jù)進行回調(diào)
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就是我們調(diào)用zk.setData時傳入的最后一個參數(shù)
System.out.println("test節(jié)點設(shè)置新數(shù) " + new String((byte[])ctx) + "成功");
break;
default:
System.out.println(KeeperException.create(Code.get(rc), path));
}
}
};
//刪除節(jié)點進行回調(diào)
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é)點是否存在進行回調(diào)
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;
}
}
};
//異步調(diào)用
@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 {
//測試完成后關(guān)閉連接
zk.close();
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。