public async Task SubscribeChildrenChangeTest()
        {
            var path  = $"/{DateTime.Now:yyyy_MM_dd_HH_mm_ss_ff}";
            var path2 = $"{path}/123";

            try
            {
                if (await _client.ExistsAsync(path))
                {
                    await _client.DeleteRecursiveAsync(path);
                }

                var types = new List <Watcher.Event.EventType>();

                var semaphore = new Semaphore(0, 2);

                await _client.SubscribeDataChange(path, (client, args) =>
                {
                    if (args.Type == Watcher.Event.EventType.NodeCreated)
                    {
                        semaphore.Release();
                    }
                    return(Task.CompletedTask);
                });

                await _client.SubscribeChildrenChange(path, (client, args) =>
                {
                    types.Add(args.Type);
                    semaphore.Release();
                    return(Task.CompletedTask);
                });

                await _client.CreatePersistentAsync(path, null);

                semaphore.WaitOne(10000);
                await _client.CreatePersistentAsync(path2, null);

                semaphore.WaitOne(10000);
                Assert.Equal(Watcher.Event.EventType.NodeChildrenChanged, types[0]);
            }
            finally
            {
                if (await _client.ExistsAsync(path))
                {
                    await _client.DeleteRecursiveAsync(path);
                }
            }
        }
Beispiel #2
0
 private void CreateBaseNode()
 {
     if (!client.ExistsAsync($"{BasePath}").Result)
     {
         try
         {
             client.CreatePersistentAsync($"{BasePath}", null).Wait();
         }
         catch (AggregateException ex)
         {
             if (!(ex.InnerException is NodeExistsException))
             {
                 throw ex;
             }
         }
     }
 }
        private static async Task CheckOrCreateRoot(IZookeeperClient zk)
        {
            bool exists = await zk.ExistsAsync(Root);

            if (!exists)
            {
                await zk.CreatePersistentAsync(Root, GetData("this is service register root"));
            }
        }
        public static async Task <IZookeeperClient> Register(ServiceInformation service)
        {
            IZookeeperClient zk = GetZooKeeper();

            await CheckOrCreateRoot(zk);

            string servicesPath = $"{Root}/{service.Key}";

            //todo 先获取后追加
            if (!await zk.ExistsAsync(servicesPath))
            {
                await zk.CreatePersistentAsync(servicesPath, GetData(string.Empty));
            }

            string serviceNodePaht = $"{servicesPath}/{service.Host}:{service.Port}";
            await zk.CreateEphemeralAsync(serviceNodePaht, GetData(service));

            return(zk);
        }
 /// <summary>
 /// 创建节点。
 /// </summary>
 /// <param name="client">ZooKeeper客户端。</param>
 /// <param name="path">节点路径。</param>
 /// <param name="data">节点数据。</param>
 /// <param name="isSequential">是否按顺序创建。</param>
 /// <returns>节点路径。</returns>
 /// <remarks>
 /// 因为使用序列方式创建节点zk会修改节点name,所以需要返回真正的节点路径。
 /// </remarks>
 public static Task <string> CreatePersistentAsync(this IZookeeperClient client, string path, byte[] data, bool isSequential = false)
 {
     return(client.CreatePersistentAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, isSequential));
 }
Beispiel #6
0
 public static Task CreatePersistentAsync(this IZookeeperClient client, string path, byte[] data)
 {
     return(client.CreatePersistentAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE));
 }