private ZKHALock(ZKClient client, string lockPach) { this.client = client; this.lockPath = lockPach; countListener = new ZKChildListener().ChildChange( (parentPath, currentChilds) => { if (Check(currentSeq, currentChilds)) { semaphore.Release(); } }); stateListener = new ZKStateListener().StateChanged( (state) => { if (state == KeeperState.SyncConnected) { //如果重新连接 //如果重连后之前的节点已删除,并且lock处于等待状态,则重新创建节点,等待获得lock if (!client.Exists(lockPach + "/" + currentSeq) && !semaphore.WaitOne(1000)) { string newPath = client.Create(lockPath + "/1", null, CreateMode.EphemeralSequential); string[] paths = newPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); currentSeq = paths[paths.Length - 1]; } } }); }
public void UnSubscribeChildChanges(string path, IZKChildListener childListener) { ConcurrentHashSet <IZKChildListener> listeners; _childListeners.TryGetValue(path, out listeners); if (listeners != null) { listeners.Remove(childListener); } }
public List <string> SubscribeChildChanges(string path, IZKChildListener listener) { //Monitor.Enter(_childListener); ConcurrentHashSet <IZKChildListener> listeners; _childListener.TryGetValue(path, out listeners); if (listeners == null) { listeners = new ConcurrentHashSet <IZKChildListener>(); _childListener.TryAdd(path, listeners); } listeners.Add(listener); //Monitor.Exit(_childListener); return(WatchForChilds(path)); }
private ZKDistributedLock(ZKClient client, string lockPach) { this.client = client; this.lockPath = lockPach; IZKChildListener childListener = new ZKChildListener().ChildChange( (parentPath, currentChilds) => { if (Check(currentSeq, currentChilds)) { semaphore.Release(); } }); this.countListener = childListener; }
public List <string> SubscribeChildChanges(string path, IZKChildListener listener) { ConcurrentHashSet <IZKChildListener> listeners; _childListeners.TryGetValue(path, out listeners); if (listeners == null) { listeners = new ConcurrentHashSet <IZKChildListener>(); _childListeners.TryAdd(path, listeners); } listeners.Add(listener); return(Task.Run(async() => { return await WatchForChildsAsync(path); }).ConfigureAwait(false).GetAwaiter().GetResult()); }
public ZKDistributedLock(ZKClient zkClient, string lockPach) { if (!(zkClient.ExistsAsync(lockPach).GetAwaiter().GetResult())) { throw new NoNodeException($"The lockPath is not exists!,please create the node.[path:{_lockPath}]"); } this._zkClient = zkClient; this._lockPath = lockPach; this.countListener = new ZKChildListener(); this.countListener.ChildChangeHandler = async(parentPath, currentChilds) => { var check = await Task.Run(() => Check(currentSeq, currentChilds)); if (check) { semaphore.Release(); } }; this._zkClient.SubscribeChildChanges(_lockPath, countListener); }