ZooKeeper入门(三)zookeeper的Watcher机制
生活随笔
收集整理的這篇文章主要介紹了
ZooKeeper入门(三)zookeeper的Watcher机制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
process方法是Watcher接口中的一個回調方法,當ZooKeeper向客戶端發送一個Watcher事件通知時,客戶端就會對相應的process方法進行回調,從而實現對事件的處理。
import java.util.concurrent.CountDownLatch; 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;public class WatcherZookeeper implements Watcher{// 集群連接地址private static final String CONNECT_ADDRES = "127.0.0.1:2181";// 會話超時時間private static final int SESSIONTIME = 2000;// 信號量,讓zk在連接之前等待,連接成功后才能往下走.private static final CountDownLatch countDownLatch = new CountDownLatch(1);private static String LOG_MAIN = "【main】 ";private ZooKeeper zk;public void createConnection(String connectAddres, int sessionTimeOut) {try {zk = new ZooKeeper(connectAddres, sessionTimeOut, this);System.out.println(LOG_MAIN + "zk 開始啟動連接服務器....");countDownLatch.await();} catch (Exception e) {e.printStackTrace();}}public boolean createPath(String path, String data) {try {this.exists(path, true);this.zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);System.out.println(LOG_MAIN + "節點創建成功, Path:" + path + ",data:" + data);} catch (Exception e) {e.printStackTrace();return false;}return true;}/*** 判斷指定節點是否存在* * @param path* 節點路徑*/public Stat exists(String path, boolean needWatch) {try {return this.zk.exists(path, needWatch);} catch (Exception e) {e.printStackTrace();return null;}}public boolean updateNode(String path,String data) throws KeeperException, InterruptedException {exists(path, true);this.zk.setData(path, data.getBytes(), -1);return false;}public void process(WatchedEvent watchedEvent) {// 獲取事件狀態KeeperState keeperState = watchedEvent.getState();// 獲取事件類型EventType eventType = watchedEvent.getType();// zk 路徑String path = watchedEvent.getPath();System.out.println("進入到 process() keeperState:" + keeperState + ", eventType:" + eventType + ", path:" + path);// 判斷是否建立連接if (KeeperState.SyncConnected == keeperState) {if (EventType.None == eventType) {// 如果建立建立成功,讓后程序往下走System.out.println(LOG_MAIN + "zk 建立連接成功!");countDownLatch.countDown();} else if (EventType.NodeCreated == eventType) {System.out.println(LOG_MAIN + "事件通知,新增node節點" + path);} else if (EventType.NodeDataChanged == eventType) {System.out.println(LOG_MAIN + "事件通知,當前node節點" + path + "被修改....");}else if (EventType.NodeDeleted == eventType) {System.out.println(LOG_MAIN + "事件通知,當前node節點" + path + "被刪除....");}}System.out.println("--------------------------------------------------------");}public static void main(String[] args) throws KeeperException, InterruptedException {WatcherZookeeper zkClientWatcher = new WatcherZookeeper();zkClientWatcher.createConnection(CONNECT_ADDRES, SESSIONTIME);boolean createResult = zkClientWatcher.createPath("/qushenWatcher", "pa-644064");}}?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的ZooKeeper入门(三)zookeeper的Watcher机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 盒马app怎么切换到门店
- 下一篇: 凄风谷地攻略最后boss怎么去 汉典凄字