-
JAR包
public class ZooKeeperDemo implements Watcher{
private static final int SESSION_TIMEOUT = 3000;
public static ZooKeeper ZooKeeper;
public static void main(String[] args) {
String path = "/mynode";
try {
ZooKeeper = new ZooKeeper("192.168.2.2:2181",SESSION_TIMEOUT,new ZooKeeperDemo());
ZooKeeper.exists(path,true);
ZooKeeper.create(path,"mycontent 1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent event) {
if(Event.KeeperState.SyncConnected == event.getState()){
if(Event.EventType.NodeCreated == event.getType()){
//創建成功時在此處進行回調
System.out.println("node創建成功!");
try {
ZooKeeper.exists(event.getPath(),true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class ZookeeperDemo01 implements Watcher {
//定義sessioTimeout常量
private static final int SESSION_TIMEOUT = 30000;
public static ZooKeeper zooKeeper;
public static void main(String[] args) {
createNode("/zknode1");
getContent("/zknode1");
setContent("/zknode1","你好!");
getContent("/zknode1");
deleteNode("/zknode1");
closeConnect(zooKeeper);
}
@Override
public void process(WatchedEvent watchedEvent) {
if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
if (Event.EventType.NodeCreated == watchedEvent.getType()) {
//創建節點的消息通知
System.out.println("創建節點成功! 時間:" + new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date()));
try {
zooKeeper.exists(watchedEvent.getPath(), true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if(Event.EventType.NodeDeleted == watchedEvent.getType()){
try {
zooKeeper.exists(watchedEvent.getPath(),true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("節點被刪除! 時間:" + new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date()));
} else if(Event.EventType.NodeDataChanged == watchedEvent.getType()){
try {
zooKeeper.exists(watchedEvent.getPath(),true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("節點被修改! 時間:" + new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date()));
}
}
}
/**
* 創建節點
*
* @param path
*/
public static void createNode(String path) {
try {
zooKeeper = new ZooKeeper("192.168.2.4:2181", SESSION_TIMEOUT, new ZookeeperDemo01());
zooKeeper.exists(path, true);
//創建節點
zooKeeper.create(path, "xiesheng".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取節點的內容
*
* @param path
* @return
*/
public static String getContent(String path) {
try {
if (zooKeeper == null) {
zooKeeper = new ZooKeeper("192.168.2.4:2181", SESSION_TIMEOUT, new ZookeeperDemo01());
}
byte[] rb = zooKeeper.getData(path, null, null);
System.out.println("獲取節點【" + path + "】的內容爲:" + new String(rb));
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 設置節點的內容
* @param path
* @param content
*/
public static void setContent(String path,String content) {
try {
if (zooKeeper == null) {
zooKeeper = new ZooKeeper("192.168.2.4:2181", SESSION_TIMEOUT, new ZookeeperDemo01());
}
zooKeeper.setData(path, content.getBytes(), -1);
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 刪除節點
* @param path
*/
public static void deleteNode(String path){
try {
if (zooKeeper == null) {
zooKeeper = new ZooKeeper("192.168.2.4:2181", SESSION_TIMEOUT, new ZookeeperDemo01());
}
zooKeeper.delete(path,-1);
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 關閉連接
* @param zooKeeper
*/
public static void closeConnect(ZooKeeper zooKeeper){
if(zooKeeper == null){
System.out.println("未連接,無需關閉!");
} else {
try {
zooKeeper.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("連接關閉成功!");
}
}
}