Exemple #1
0
 /// <summary>
 /// 删除并返回顶部元素
 /// </summary>
 /// <returns></returns>
 public T Poll()
 {
     while (true)
     {
         Element element = GetFirstElement();
         if (element == null)
         {
             return(default(T));
         }
         try
         {
             bool flag = _zkClient.Delete(element.name);
             if (flag)
             {
                 return(element.data);
             }
             else
             {
                 //如果删除失败,证明已被其他线程获取
                 //重新获取最新的元素,直到获取成功为止。
             }
         }
         catch (ZKNoNodeException e)
         {
         }
         catch (Exception e)
         {
             throw ExceptionUtil.ConvertToException(e);
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// 获取队列顶部元素
        /// </summary>
        /// <returns></returns>
        private Element GetFirstElement()
        {
            try
            {
                while (true)
                {
                    List <string> list = _zkClient.GetChildren(_root);
                    if (list.Count == 0)
                    {
                        return(null);
                    }
                    string elementName = GetSmallestElement(list);

                    try
                    {
                        return(new Element(_root + "/" + elementName, _zkClient.ReadData <T>(_root + "/" + elementName)));
                    }
                    catch (ZKNoNodeException e)
                    {
                        // somebody else picked up the element first, so we have to
                        // retry with the new first element
                    }
                }
            }
            catch (Exception e)
            {
                throw ExceptionUtil.ConvertToException(e);
            }
        }
Exemple #3
0
 /// <summary>
 /// 添加一个元素
 /// </summary>
 /// <param name="element"></param>
 /// <returns></returns>
 public bool Offer(T element)
 {
     try
     {
         _zkClient.CreatePersistentSequential(_root + "/" + ELEMENT_NAME + "-", element);
     }
     catch (Exception e)
     {
         throw ExceptionUtil.ConvertToException(e);
     }
     return(true);
 }
Exemple #4
0
        public T RetryUntilConnected <T>(Func <T> callable)
        {
            if (_zookeeperEventThread != null && Thread.CurrentThread == _zookeeperEventThread)
            {
                throw new Exception("Must not be done in the zookeeper event thread.");
            }
            long operationStartTime = DateTime.Now.ToUnixTime();

            while (true)
            {
                if (_closed)
                {
                    throw new Exception("ZKClient already closed!");
                }
                try
                {
                    return(callable());
                }
                catch (ConnectionLossException e)
                {
                    // we give the event thread some time to update the status to 'Disconnected'
                    Thread.Yield();
                    WaitForRetry();
                }
                catch (SessionExpiredException e)
                {
                    // we give the event thread some time to update the status to 'Expired'
                    Thread.Yield();
                    WaitForRetry();
                }
                catch (KeeperException e)
                {
                    throw ZKException.Create(e);
                }
                catch (ThreadInterruptedException e)
                {
                    throw new ZKInterruptedException(e);
                }
                catch (Exception e)
                {
                    throw ExceptionUtil.ConvertToException(e);
                }
                // before attempting a retry, check whether retry timeout has elapsed
                if (_operationRetryTimeoutInMillis > -1 &&
                    (DateTime.Now.ToUnixTime() - operationStartTime) >= _operationRetryTimeoutInMillis)
                {
                    throw new ZKTimeoutException("Operation cannot be retried because of retry timeout (" + _operationRetryTimeoutInMillis + " milli seconds)");
                }
            }
        }