internal async Task HandleNodeDataChange(IZookeeperClient client, NodeDataChangeArgs args) { var eventType = args.Type; byte[] nodeData = null; if (args.CurrentData != null && args.CurrentData.Any()) { nodeData = args.CurrentData.ToArray(); } switch (eventType) { case Watcher.Event.EventType.NodeDeleted: var serviceId = Path.Split("/").Last(); _serviceRouteCache.RemoveCache(serviceId); break; case Watcher.Event.EventType.NodeCreated: Check.NotNullOrEmpty(nodeData, nameof(nodeData)); var createdJsonString = nodeData.GetString(); var createdServiceRouteDescriptor = _serializer.Deserialize <ServiceRouteDescriptor>(createdJsonString); _serviceRouteCache.UpdateCache(createdServiceRouteDescriptor); break; case Watcher.Event.EventType.NodeDataChanged: Check.NotNullOrEmpty(nodeData, nameof(nodeData)); var updateJsonString = nodeData.GetString(); var updateServiceRouteDescriptor = _serializer.Deserialize <ServiceRouteDescriptor>(updateJsonString); _serviceRouteCache.UpdateCache(updateServiceRouteDescriptor); break; } }
private async Task OnDataChangeHandle(WatchedEvent watchedEvent) { if (!HasDataChangeHandler) { return; } //获取当前节点最新数据的一个委托 var getCurrentData = new Func <Task <IEnumerable <byte> > >(() => _client.RetryUntilConnected(async() => { try { return(await GetDataAsync()); } catch (KeeperException.NoNodeException) //节点不存在返回null { return(null); } })); //根据事件类型构建节点变更事件参数 NodeDataChangeArgs args; switch (watchedEvent.get_Type()) { case Watcher.Event.EventType.NodeCreated: args = new NodeDataChangeArgs(Path, Watcher.Event.EventType.NodeCreated, await getCurrentData()); break; case Watcher.Event.EventType.NodeDeleted: args = new NodeDataChangeArgs(Path, Watcher.Event.EventType.NodeDeleted, null); break; case Watcher.Event.EventType.NodeDataChanged: case Watcher.Event.EventType.None: //重连时触发 args = new NodeDataChangeArgs(Path, Watcher.Event.EventType.NodeDataChanged, await getCurrentData()); break; default: throw new NotSupportedException($"不支持的事件类型:{watchedEvent.get_Type()}"); } await _dataChangeHandler(_client, args); //重新监听 await WatchDataChange(); }
//public void SetCurrentData(byte[] currentData) //{ // _currentData = currentData; //} internal async Task HandleNodeDataChange(IZookeeperClient client, NodeDataChangeArgs args) { Watcher.Event.EventType eventType = args.Type; var nodeData = new byte[0]; if (args.CurrentData != null && args.CurrentData.Any()) { nodeData = args.CurrentData.ToArray(); } switch (eventType) { case Watcher.Event.EventType.NodeCreated: _action(new byte[0], nodeData); _currentData = nodeData; break; case Watcher.Event.EventType.NodeDataChanged: _action(_currentData, nodeData); _currentData = nodeData; break; } }
private Task OnNodeDataChangeAsync(ZookeeperClient client, NodeDataChangeArgs args) { return(this.LoadRemoteConfigurationAsync(client)); }