Curator典型使用情境之事件監聽。__java

來源:互聯網
上載者:User

        ZooKeeper原生支援通過註冊Watcher來進行事件監聽,但是其使用並不是特別方便,需要開發人員自己反覆註冊Watcher,比較繁瑣。Curator引入了Cache來實現對ZooKeeper服務端事件的監聽。Cache是Curator中對事件監聽的封裝,其對事件的監聽其實可以近似看作是一個本機快取視圖和遠程ZooKeeper視圖的對比過程。同時Curator能夠自動為開發人員處理反覆註冊監聽,從而大大簡化了原生API開發的繁瑣過程。Cache分為兩類監聽類型:節點監聽和子節點監聽。 NodeCache

        NodeCache用於監聽指定ZooKeeper資料節點本身的變化,其構造方法有如下兩個: public NodeCache(CuratorFramework client, String path); public NodeCache(CuratorFramework client, String path, boolean dataIsCompressed);

        NodeCache構造方法參數說明如下表所示。

參數名 說明
client Curator用戶端執行個體
path 資料節點的節點路徑
dataIsCompressed 是否進行資料壓縮

        同時,NodeCache定義了事件處理的回調介面NodeCacheListener。 NodeCacheListener回調介面定義

public interface NodeCacheListener {

// Called when a change has occurred

public void nodeChanged() throws Exception;

}

        當資料節點的內容發生變化的時候,就會回調該方法。下面通過一個實際例子來看看如何在代碼中使用NodeCache。 NodeCache使用樣本

public class NodeCache_Sample {

static String path = "/zk-book/nodecache";

static CuratorFramework client = CuratorFrameworkFactory.builder().connectString("domain1.book.zookeeper:2181").sessionTimeoutMs(5000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();

public static void main(String[] args) throws Exception {

client.start();

client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, "init".getBytes());

final NodeCache cache = new NodeCache(client, path, false);

cache.start(true);

cache.getListenable().addListener(new NodeCacheListener() {

@Override

public void nodeChanged() throws Exception {

System.out.println("Node data update, new data: " + new String(cache.getCurrentData().getData()));

}

});

client.setData().forPath(path, "u".getBytes());

Thread.sleep(1000);

client.delete().deletingChildrenIfNeeded().forPath(path);

Thread.sleep(Integer.MAX_VALUE);

}

}

        在上面的樣本程式中,首先構造了一個NodeCache執行個體,然後調用start方法,該方法有個boolean類型的參數,預設是false,如果設定為true,那麼NodeCache在第一次啟動的時候就會立刻從ZooKeeper上讀取對應節點的資料內容,並儲存在Cache中。

        NodeCache不僅可以用於監聽資料節點的內容變更,也能監聽指定節點是否存在。如果原本節點不存在,那麼Cache就會在節點被建立後觸發NodeCacheListener。但是,如果該資料節點被刪除,那麼Curator就無法觸發NodeCacheListener了。 PathChildrenCache

        PathChildrenCache用於監聽指定ZooKeeper資料節點的子節點變化情況。

        PathChildrenCache有如下幾個構造方法的定義: public PathChildrenCache(CuratorFramework client, String path, boolean cacheData); public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, ThreadFactory threadFactory); public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean datalsCompressed, ThreadFactory threadFactory); public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean datalsCompressed, final ExecutorService executorSerivice); public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean datalsCompressed, final CloseableExecutorService executorService);

        public PathChildrenCache構造方法參數說明如下表所示。

參數名 說明
client Curator用戶端執行個體
path 資料節點的節點路徑
dataIsCompressed 是否進行資料壓縮
cacheData 用於配置是否把節點內容緩衝起來,如果配置為true,那麼用戶端在接收到節點列表變更的同時,也能夠擷取到節點的資料內容;如果配置為false,則無法擷取到節點的資料內容
threadFactory 利用這兩個參數,開發人員可以通過構造一個專門的線程池,來處理事件通知
executorService 利用這兩個參數,開發人員可以通過構造一個專門的線程池,來處理事件通知

        PathChildrenCache定義了事件處理的回調介面PathChildrenCacheListener,其定義如下。 PathChildrenListener回調介面定義

public interface PathChildrenListener { public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception;

}

        當指定節點的子節點發生變化時,就會回調該方法。PathChildrenCacheEvent類中定義了所有的事件類型,主要包括新增子節點(CHILD_ADDED)、子節點資料變更(CHILD_UPDATED)和子節點刪除(CHILD_REMOVED)三類。

        下面通過一個實際例子來看如何在代碼中使用PathChildrenCache。 PathChildrenCache使用樣本

public class PathChildrenCache_Sample {

static String path = "/zk-book/nodecache";

static CuratorFramework client = CuratorFrameworkFactory.builder().connectString("domain1.book.zookeeper:2181").sessionTimeoutMs(5000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();

public static void main(String[] args) throws Exception {

client.start();

PathChildrenCache cache = new PathChildrenCache(client, path, true);

cache.start(StartMode.POST_INITIALIZED_EVENT);

cache.getListenable().addListener(new PathChildrenCacheListener(){

public void childEvent(CuratorFramework client, PathChildrenCacheEvent event)  throws Exception{

switch(event.getType()) {

case CHILD_ADDED :

System.out.println("CHILD_ADDED," + event.getData().getPath());

break;

case CHILD_UPDATED :

System.out.println("CHILD_UPDATED," + event.getData().getPath());

break;

case CHILD_REMOVED :

System.out.println("CHILD_REMOVED," + event.getData().getPath());

break;

default:

break;

}

}

});

client.create().withMode(CreateMode.PERSISTENT).forPath(path);

Thread.sleep(1000);

client.create().withMode(Create.PERSISTENT).forPath(path + "/c1");

Thread.sleep(1000);

client.delete().forPath(path + "/c1");

Thread.sleep(1000);

client.delete().forPath(path);

Thread.sleep(Integer.MAX_VALUE);
}

}

        運行程式,輸出結果如下:

        在上面這個樣本程式中,對/zk-book節點進行了子節點變更事件的監聽,一旦該節點新增/刪除子節點,或者子節點資料發生變更,就會回調PathChildrenCacheListener,並根據對應的事件類型進行相關的處理。同時,我們也看到,對於節點zk=book本身的變更,並沒有通知到用戶端。

        另外,和其他ZooKeeper用戶端產品一樣,Curator也無法對二級子節點進行事件監聽。也就是說,如果使用PathChildrenCache對/zk-book進行監聽,那麼當/zk-book/c1/c2節點被建立或刪除的時候,是無法觸發子節點變更事件的。

哈哈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.