国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ZooKeeper(3.4.5)

2019-11-14 23:07:34
字體:
來源:轉載
供稿:網友
ZooKeeper(3.4.5) - 使用 Curator(2.7.0) 監聽事件

ZooKeeper原生的API支持通過注冊Watcher來進行事件監聽,但是Watcher通知是一次性的,因此開發過程中需要反復注冊Watcher,比較繁瑣。Curator引入了Cache來監聽ZooKeeper服務端的事件。Cache對ZooKeeper事件監聽進行了封裝,能夠自動處理反復注冊監聽,簡化了ZooKeeper原生API繁瑣的開發過程。

簡單的示例:

package com.huey.dream.demo;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.framework.recipes.cache.NodeCache;import org.apache.curator.framework.recipes.cache.NodeCacheListener;import org.apache.curator.framework.recipes.cache.PathChildrenCache;import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;import org.apache.curator.retry.ExponentialBackoffRetry;/** * Curator事件監聽 * @author  huey * @version 1.0  * @created 2015-3-2 */public class CarutorDemo {    public static void main(String[] args) throws Exception {        CuratorFramework client = CuratorFrameworkFactory.builder()            .connectString("192.168.1.109:2181")            .sessionTimeoutMs(5000)            .connectionTimeoutMs(3000)            .retryPolicy(new ExponentialBackoffRetry(1000, 3))            .build();        client.start();                client.create()            .creatingParentsIfNeeded()            .forPath("/zk-huey/cnode", "hello".getBytes());                /**         * 在注冊監聽器的時候,如果傳入此參數,當事件觸發時,邏輯由線程池處理         */        ExecutorService pool = Executors.newFixedThreadPool(2);                /**         * 監聽數據節點的變化情況         */        final NodeCache nodeCache = new NodeCache(client, "/zk-huey/cnode", false);        nodeCache.start(true);        nodeCache.getListenable().addListener(            new NodeCacheListener() {                @Override                public void nodeChanged() throws Exception {                    System.out.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 启东市| 略阳县| 丹棱县| 扬州市| 漠河县| 皮山县| 阜南县| 延边| 正镶白旗| 长海县| 雷波县| 台中县| 城步| 盘山县| 习水县| 闽清县| 正镶白旗| 鹤庆县| 文成县| 紫阳县| 绥化市| 美姑县| 新建县| 台中市| 咸宁市| 旬阳县| 延长县| 阿拉善右旗| 南宁市| 晋江市| 海兴县| 东莞市| 资溪县| 永年县| 贡觉县| 高唐县| 铁岭市| 鹰潭市| 肥西县| 蒲城县| 山西省|