/// <summary> /// 执行给定的NodePath的child变化事件 /// </summary> /// <param name="path">nodePath</param> /// <param name="childListeners">child变化监听器集合</param> private void FireChildChangedEvents(string path, ConcurrentHashSet <IZkChildListener> childListeners) { try { //重新注册子节点变化监听器 foreach (var l in childListeners) { var listener = l; _eventThread.Send(new ZkEvent("Children of " + path + " changed sent to " + listener) { RunAction = () => { try { // if the node doesn't exist we should listen for the root node to reappear Exists(path); var children = this.GetChildren(path); listener.HandleChildChange(path, children); } catch (ZkNoNodeException) { listener.HandleChildChange(path, null); } } }); } } catch (Exception e) { Logger.Error("Failed to fire child changed event. Unable to getChildren. ", e); } }
public void Run() { System.Diagnostics.Debug.WriteLine("Starting ZkClient event thread."); try { while (!this.tokenSource.IsCancellationRequested) { ZkEvent zkEvent = this._events.Take(); int eventId = this._eventId.GetAndIncrement(); System.Diagnostics.Debug.WriteLine("Delivering event #" + eventId + " " + zkEvent); try { zkEvent.RunAction(); } catch (ThreadInterruptedException) { this.tokenSource.Cancel(); } catch (Exception e) { Logger.Error("Error handling event " + zkEvent, e); } System.Diagnostics.Debug.WriteLine("Delivering event #" + eventId + " done"); } } catch (ThreadInterruptedException) { System.Diagnostics.Debug.WriteLine("Terminate ZkClient event thread."); } }