Created
January 27, 2016 14:34
-
-
Save nj4x/a13c7b8c4362ded9cedc to your computer and use it in GitHub Desktop.
ZooKeeperNetEx.Recipes WriteLock allows multiple clients to obtain lock to the same dir simultaneously
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using org.apache.zookeeper; | |
using org.apache.zookeeper.recipes.@lock; | |
namespace ZooKeeperNetExTest | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Test().Wait(); | |
} | |
private static async Task Test() | |
{ | |
var zk1 = new ZooKeeper("localhost:2181", 5000, new ZkWatcher()); // app1 | |
var zk2 = new ZooKeeper("localhost:2181", 5000, new ZkWatcher()); // app2 | |
var zk1Lock = new WriteLock(zk1, "/test", null, null); | |
var zk2Lock = new WriteLock(zk2, "/test", null, null); | |
await zk2Lock.Lock().ConfigureAwait(false); | |
await Task.Delay(100).ConfigureAwait(false); | |
await zk1Lock.Lock().ConfigureAwait(false); | |
await Task.Delay(100).ConfigureAwait(false); | |
Console.WriteLine("App1 lock acquired: " + zk1Lock.Owner); | |
Console.WriteLine("App2 lock acquired: " + zk2Lock.Owner); | |
var childrenResult = await zk1.getChildrenAsync("/test").ConfigureAwait(false); | |
Console.WriteLine("ls /test"); | |
foreach (var @lock in childrenResult.Children) | |
{ | |
Console.WriteLine("" + @lock); | |
} | |
Console.ReadLine(); | |
} | |
private class ZkWatcher : Watcher | |
{ | |
public override Task process(WatchedEvent @event) | |
{ | |
Console.WriteLine(@event.ToString()); | |
return Task.Delay(0); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment