Ejemplo n.º 1
0
        public void TestDataChanges1()
        {
            LOG.Info("--- testDataChanges1");
            string          path   = "/a";
            Holder <string> holder = new Holder <string>();

            IZKDataListener listener = new ZKDataListener()
                                       .DataCreatedOrChange((dataPath, data) =>
            {
                holder.value = Convert.ToString(data);
            })
                                       .DataDeleted((dataPath) =>
            {
                holder.value = null;
            });

            _zkClient.SubscribeDataChanges(path, listener);
            _zkClient.CreatePersistent(path, "aaa");

            // wait some time to make sure the event was triggered

            string contentFromHolder = TestUtil.WaitUntil("b", () => { return(holder.value); }, new TimeSpan(0, 0, 0, 5));

            Assert.True("aaa" == contentFromHolder);
        }
Ejemplo n.º 2
0
        public void TestDataChanges2()
        {
            LOG.Info("--- testDataChanges2");
            string path         = "/a";
            int    countChanged = 0;
            int    countDeleted = 0;

            IZKDataListener listener = new ZKDataListener()
                                       .DataCreatedOrChange((dataPath, data) =>
            {
                Interlocked.Increment(ref countChanged);
            })
                                       .DataDeleted((dataPath) =>
            {
                Interlocked.Decrement(ref countDeleted);
            });


            _zkClient.SubscribeDataChanges(path, listener);

            // create node
            _zkClient.CreatePersistent(path, "aaa");

            // wait some time to make sure the event was triggered
            TestUtil.WaitUntil(1, () => { return(countChanged); }, new TimeSpan(0, 0, 0, 5));
            Assert.True(1 == countChanged);
            Assert.True(0 == countDeleted);


            countChanged = 0;
            countDeleted = 0;

            // delete node, this should trigger a delete event
            _zkClient.Delete(path);
            // wait some time to make sure the event was triggered
            TestUtil.WaitUntil(1, () => { return(countDeleted); }, new TimeSpan(0, 0, 0, 5));
            Assert.True(0 == countChanged);
            Assert.True(1 == countDeleted);

            // test if watch was reinstalled after the file got deleted
            countChanged = 0;
            _zkClient.CreatePersistent(path, "aaa");

            // wait some time to make sure the event was triggered
            TestUtil.WaitUntil(1, () => { return(countChanged); }, new TimeSpan(0, 0, 0, 5));
            Assert.True(1 == countChanged);

            // test if changing the contents notifies the listener
            _zkClient.WriteData(path, "bbb");

            // wait some time to make sure the event was triggered
            TestUtil.WaitUntil(2, () => { return(countChanged); }, new TimeSpan(0, 0, 0, 5));
            Assert.True(2 == countChanged);
        }
Ejemplo n.º 3
0
        public static void Init(params string[] appIds)
        {
            SyncVersion(appIds);  //先同步获取一次,保证global之后执行的代码的都能获取到配置

            if (zkClient != null) //如果zookeeper可用,则注册zookeeper
            {
                ZKDataListener zkDataListener = new ZKDataListener()
                                                .DataChange((dataPath, data) =>
                {
                    SyncVersion((dataPath.Split('/'))[2]);
                });
                foreach (var appId in appIds)
                {
                    zkClient.SubscribeDataChanges("/ConfigCenter/" + appId, zkDataListener);
                }
            }

            //_task = new Task(SyncVersion, appId, 20000, 10000);  //如果zookeeper不可用,同时为了保证在zookeeper挂掉后,有补偿措施,启动一个定时获取的任务。
        }
Ejemplo n.º 4
0
 public void SubscribeDataChanges()
 {
     using (ZKClient zkClient = ZKClientBuilder.NewZKClient(TestUtil.zkServers).Build())
     {
         IZKDataListener dataListener = new ZKDataListener();
         // 节点创建和节点内容变化
         dataListener.DataCreatedOrChangeHandler = async(dataPath, data) =>
         {
             await Task.Run(() =>
             {
                 Console.WriteLine(dataPath + ":" + Convert.ToString(data));
             });
         };
         // 节点删除
         dataListener.DataDeletedHandler = async(dataPath) =>
         {
             await Task.Run(() =>
             {
                 Console.WriteLine(dataPath);
             });
         };
         // 节点创建
         dataListener.DataCreatedHandler = async(dataPath, data) =>
         {
             await Task.Run(() =>
             {
                 Console.WriteLine(dataPath + ":" + Convert.ToString(data));
             });
         };
         // 节点内容变化
         dataListener.DataChangeHandler = async(dataPath, data) =>
         {
             await Task.Run(() =>
             {
                 Console.WriteLine(dataPath);
             });
         };
         zkClient.SubscribeDataChanges("/testUserNode", dataListener);
         Thread.Sleep(TimeSpan.FromSeconds(60));
     }
 }
Ejemplo n.º 5
0
        public void SubscribeDataChanges()
        {
            ZKClient zkClient = ZKClientBuilder.NewZKClient(TestUtil.zkServers)
                                .SessionTimeout(10000)
                                .ConnectionTimeout(10000)
                                .Serializer(new SerializableSerializer())
                                .Build();

            Console.WriteLine("conneted ok!");
            IZKDataListener dataListener = new ZKDataListener()
                                           .DataCreatedOrChange((dataPath, data) =>
            {
                Console.WriteLine(dataPath + ":" + Convert.ToString(data));
            })
                                           .DataDeleted((dataPath) =>
            {
                Console.WriteLine(dataPath);
            });

            zkClient.SubscribeDataChanges("/testUserNode", dataListener);
            Thread.Sleep(int.MaxValue);
            zkClient.Close();
            zkClient = null;
        }