java 连接 zookeeper

package com.huawei;

import java.util.List;
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.PathChildrenCache.StartMode;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class CarutorDemo
{
    
    public static void main(String[] args)
        throws Exception
    {
        CuratorFramework client =
            CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
        client.start();
        List<String> lists = client.getChildren().forPath("/zk-huey/");
        for (int i = 0; i < lists.size(); i++)
        {
            System.out.println(lists.get(i));
        }
        
        try
        {
            client.create()
                .creatingParentsIfNeeded()
                .forPath("/zk-huey/PASS_ISERVER_NOMQ_PRODUCE",
                    "false".getBytes());
        }
        catch (Exception e)
        {
            System.out.println("A error is : " + e.toString());
        }
        try
        {
            client.create()
                .creatingParentsIfNeeded()
                .forPath("/zk-huey/PASS_ISERVER_NOMQ_CONSUME",
                    "false".getBytes());
        }
        catch (Exception e)
        {
            System.out.println("B error is : " + e.toString());
        }
        
        /**
         * 在注册监听器的时候,如果传入此参数,当事件触发时,逻辑由线程池处理
         */
        ExecutorService pool = Executors.newFixedThreadPool(2);
        
        /**
         * 监听数据节点的变化情况
         */
        final NodeCache nodeCache =
            new NodeCache(client, "/zk-huey/PASS_ISERVER_NOMQ_CONSUME", false);
        nodeCache.start(true);
        nodeCache.getListenable().addListener(new NodeCacheListener()
        {
            @Override
            public void nodeChanged()
                throws Exception
            {
                System.out.println("A Node data is changed, new data: "
                    + new String(nodeCache.getCurrentData().getData()));
            }
        },
            pool);
        
        final NodeCache anodeCache =
            new NodeCache(client, "/zk-huey/PASS_ISERVER_NOMQ_PRODUCE", false);
        anodeCache.start(true);
        anodeCache.getListenable().addListener(new NodeCacheListener()
        {
            @Override
            public void nodeChanged()
                throws Exception
            {
                System.out.println("B Node data is changed, new data: "
                    + new String(nodeCache.getCurrentData().getData()));
            }
        },
            pool);
        
        /**
         * 监听子节点的变化情况
         */
        final PathChildrenCache childrenCache =
            new PathChildrenCache(client, "/zk-huey", true);
        childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
        childrenCache.getListenable()
            .addListener(new PathChildrenCacheListener()
            {
                @Override
                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_REMOVED:
                            System.out.println("CHILD_REMOVED: "
                                + event.getData().getPath());
                            break;
                        case CHILD_UPDATED:
                            System.out.println("CHILD_UPDATED: "
                                + event.getData().getPath());
                            break;
                        default:
                            break;
                    }
                }
            },
                pool);
        
        client.setData().forPath("/zk-huey/PASS_ISERVER_NOMQ_PRODUCE",
            "true".getBytes());
        client.setData().forPath("/zk-huey/PASS_ISERVER_NOMQ_CONSUME",
            "true".getBytes());
        
        // Thread.sleep(100 * 1000);
        Thread.sleep(3000);
        pool.shutdown();
        client.close();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值