码迷,mamicode.com
首页 > Windows程序 > 详细

Curator api的使用

时间:2015-09-09 15:00:30      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

zookeeper在java上的客户端用起来非常繁琐,而且watcher只能触发一次。所以有了ZkClient和Curator。Curator特点就是可以使用Fluent风格来写代码。

今天把Curator的api简单的用了一下。

1.curator对节点的操作  需要curator-framework Jar包

<dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
 </dependency>
 (String[] args) {
        String servers = RetryPolicy retryPolicy = ExponentialBackoffRetry()CuratorFramework curator = CuratorFrameworkFactory.()
                .connectString(servers)
                .retryPolicy(retryPolicy)
                .sessionTimeoutMs()
                        .build()curator.start(){
            curator.create().creatingParentsIfNeeded().inBackground(BackgroundCallback() {
                (CuratorFramework curatorFrameworkCuratorEvent curatorEvent) Exception {
                    System..println(curatorEvent)System..println(curatorFramework)}
            }).forPath(.getBytes())} (Exception e) {
            e.printStackTrace()}
    }

    MyCallback BackgroundCallback {

        (CuratorFramework curatorFrameworkCuratorEvent curatorEvent) Exception {
            System..println(curatorEvent)System..println(curatorFramework)}
    }


2.对节点以及子节点的监听 需要curator-recipes  jar报

org.apache.curatorcurator-recipes2.8.0
(String[] args) {
        String servers = RetryPolicy retryPolicy = ExponentialBackoffRetry()CuratorFramework curator = CuratorFrameworkFactory.(serversretryPolicy)curator.start(){
            PathChildrenCache childCache = PathChildrenCache(curator)childCache.start()childCache.getListenable().addListener(PathChildrenCacheListener() {
                (CuratorFramework curatorFrameworkPathChildrenCacheEvent pathChildrenCacheEvent) Exception {
                    System..println()System..println(pathChildrenCacheEvent.toString())}
            })TimeUnit..sleep()} (Exception e) {
            e.printStackTrace()}
    }


Curator api的使用

标签:

原文地址:http://my.oschina.net/u/2250599/blog/503787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!