Example #1
0
 private static ZooKeeper CreateClient(ZkOption option)
 {
     if (_option == null)
     {
         throw new Exception("it is not initial");
     }
     _option = option;
     return(option.SessionId == long.MinValue
         ? new ZooKeeper(option.ConnectionsString, option.SessionTimeout, ZkWatcher.Default, option.CanBeReadOnly)
         : new ZooKeeper(option.ConnectionsString, option.SessionTimeout, ZkWatcher.Default, option.SessionId,
                         option.SessionPassword, option.CanBeReadOnly));
 }
Example #2
0
 public static void Configure(ZkOption option)
 {
     lock (_lockObj)
     {
         if (Client == null)
         {
             _option = option;
             Client  = CreateClient(option);
             Client.existsAsync("/locks").Wait();
         }
     }
 }
Example #3
0
 public ZkLocker(string lockName, ZkOption option, int lockTimeout = 5000)
 {
     _client = option.SessionId == long.MinValue
         ? new ZooKeeper(option.ConnectionsString, option.SessionTimeout, this, option.CanBeReadOnly)
         : new ZooKeeper(option.ConnectionsString, option.SessionTimeout, this, option.SessionId,
                         option.SessionPassword, option.CanBeReadOnly);
     if (_client.existsAsync("/locks").Result == null)
     {
         _client.createAsync("/locks", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait();
     }
     if (_client.existsAsync("/locks/" + lockName).Result == null)
     {
         _client.createAsync("/locks/" + lockName, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait();
     }
     _lockName    = lockName;
     _lockTimeout = lockTimeout;
 }
Example #4
0
 /// <summary>
 /// 全局默认锁defaultLock
 /// </summary>
 /// <param name="option">配置选项</param>
 /// <param name="lockTimeout">设置锁超时时间</param>
 public ZkLocker(ZkOption option, int lockTimeout) : this("defaultLock", option, lockTimeout)
 {
 }
Example #5
0
 /// <summary>
 /// 全局默认锁defaultLock
 /// </summary>
 /// <param name="option">配置选项</param>
 public ZkLocker(ZkOption option) : this("defaultLock", option)
 {
 }