public async Task StartAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var existPath = await _client.ExistsAsync(ListenerPath);

            if (!existPath)
            {
                await _client.CreateRecursiveAsync(ListenerPath, Encoding.UTF8.GetBytes("Bucket.Listener"));
            }
            await _client.SubscribeDataChange(ListenerPath, async (ct, args) =>
            {
                if (!_isSubscribe)
                {
                    return;
                }
                var currentData = Encoding.UTF8.GetString(args.CurrentData.ToArray());
                if (!string.IsNullOrWhiteSpace(currentData))
                {
                    var command = JsonConvert.DeserializeObject <Values.NetworkCommand>(currentData);
                    if (args.Path.ToLower() == ListenerPath.ToLower())
                    {
                        switch (args.Type)
                        {
                        case Watcher.Event.EventType.NodeDataChanged:
                            await _extractCommand.ExtractCommandMessage(command);
                            break;
                        }
                    }
                }
            });

            _isSubscribe = true;
        }
Beispiel #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="identity">当前锁的唯一标识</param>
 public ZKLockProvider(string identity)
     : base(identity)
 {
     ZKConfig  = ZKConfig.New();
     _zkClient = new ZookeeperClient(ZKConfig.ConnStr);
     if (_zkClient.ExistsAsync(NodeName).Result == false)
     {
         _zkClient.CreateRecursiveAsync(NodeName, ZKConfig.LockZKValue.Bytes());
     }
 }
Beispiel #3
0
        public async Task PublishCommandMessage(string applicationCode, NetworkCommand command)
        {
            var ListenerPath = $"/Bucket.Listener/{applicationCode}";
            var existPath    = await _client.ExistsAsync(ListenerPath);

            if (!existPath)
            {
                await _client.CreateRecursiveAsync(ListenerPath, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(command)));
            }
            await _client.SetDataAsync(ListenerPath, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(command)));
        }
 /// <summary>
 /// 创建节点
 /// </summary>
 /// <param name="node">节点名称</param>
 /// <param name="value">节点数据</param>
 /// <returns>Task</returns>
 public Task CreateNode(string node, string value)
 {
     if (string.IsNullOrWhiteSpace(node) || node.StartsWith("/") == false)
     {
         return(Task.FromException(new Exception("无效的node")));
     }
     if (string.IsNullOrWhiteSpace(value))
     {
         return(Task.FromException(new Exception("无效的value")));
     }
     return(_zkClient.CreateRecursiveAsync(node, Encoding.UTF8.GetBytes(value)));
 }
        public async Task CreateRecursiveAndDeleteRecursiveTest()
        {
            var pathRoot = $"/{DateTime.Now:yyyy_MM_dd_HH_mm_ss_ff}";
            var path     = $"{pathRoot}/1/2";

            if (await _client.ExistsAsync(pathRoot))
            {
                await _client.DeleteRecursiveAsync(pathRoot);
            }

            await _client.CreateRecursiveAsync(path, null);

            Assert.True(await _client.ExistsAsync(path));

            await _client.DeleteRecursiveAsync(pathRoot);

            if (await _client.ExistsAsync(pathRoot))
            {
                throw new Exception("删除失败");
            }
        }
 /// <summary>
 /// 递归创建该节点下的所有子节点和该节点本身。
 /// </summary>
 /// <param name="client">ZooKeeper客户端。</param>
 /// <param name="path">节点路径。</param>
 /// <param name="data">节点数据。</param>
 /// <param name="acls">权限。</param>
 public static Task CreateRecursiveAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls)
 {
     return(client.CreateRecursiveAsync(path, p => data, p => acls));
 }
 /// <summary>
 /// 递归创建该节点下的所有子节点和该节点本身。
 /// </summary>
 /// <param name="client">ZooKeeper客户端。</param>
 /// <param name="path">节点路径。</param>
 /// <param name="data">节点数据。</param>
 public static Task CreateRecursiveAsync(this IZookeeperClient client, string path, byte[] data)
 {
     return(client.CreateRecursiveAsync(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE));
 }