/// <summary>
        /// 返回null表示连接不成功
        /// </summary>
        /// <param name="authEnum"></param>
        /// <param name="authInfo"></param>
        /// <returns></returns>
        public ZooKeeper Connect(AuthEnum authEnum, string authInfo)
        {
            foreach (string address in _address)
            {
                _zooKeeper = new ZooKeeper(address, _sessiontTimeout, new DefaultWatcher());
                if (authEnum != AuthEnum.world)
                {
                    _zooKeeper.addAuthInfo(authEnum.ToString(), System.Text.Encoding.UTF8.GetBytes(authInfo));
                }
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (stopwatch.ElapsedMilliseconds < _connectTimeout / _address.Count)
                {
                    ZooKeeper.States states = _zooKeeper.getState();
                    if (states == ZooKeeper.States.CONNECTED || states == ZooKeeper.States.CONNECTEDREADONLY)
                    {
                        break;
                    }
                }
                stopwatch.Stop();
                if (_zooKeeper.getState().ToString().ToUpper().Contains("CONNECTED"))
                {
                    break;
                }
            }

            return(_zooKeeper);
        }
        /// <summary>
        /// 得到连接状态
        /// </summary>
        /// <returns></returns>
        public string GetState()
        {
            if (_zooKeeper != null)
            {
                ZooKeeper.States states = _zooKeeper.getState();
                return(states.ToString());
            }

            return(_fail);
        }
        /// <summary>
        /// 是否已经连接
        /// </summary>
        /// <returns></returns>
        public bool Connected()
        {
            if (_zooKeeper != null)
            {
                ZooKeeper.States states = _zooKeeper.getState();
                if (states == ZooKeeper.States.CONNECTED || states == ZooKeeper.States.CONNECTEDREADONLY)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #4
0
         /// <summary>
         /// 得到连接状态
         /// </summary>
         /// <returns></returns>
         public string GetState()
 {
     try
     {
         if (_zooKeeper != null)
         {
             ZooKeeper.States states = _zooKeeper.getState();
             return(states.ToString());
         }
     }
     catch (Exception ex)
     {
         _log.ErrorFormat("获取zookeeper连接状态发生异常:{0}", ex.Message + ex.StackTrace);
     }
     return(_fail);
 }
Example #5
0
         /// <summary>
         /// 是否已经连接
         /// </summary>
         /// <returns></returns>
         public bool Connected()
 {
     try
     {
         if (_zooKeeper != null)
         {
             ZooKeeper.States states = _zooKeeper.getState();
             if (states == ZooKeeper.States.CONNECTED || states == ZooKeeper.States.CONNECTEDREADONLY)
             {
                 return(true);
             }
         }
     }
     catch (Exception ex)
     {
         _log.ErrorFormat("获取zookeeper连接状态发生异常:{0}", ex.Message + ex.StackTrace);
     }
     return(false);
 }
        public void Connect(long maxMsToWaitUntilConnected, IWatcher watcher)
        {
            bool started = false;

            try
            {
                EventLock.LockInterruptibly();
                ShutdownTrigger = false;
                _eventThread    = new ZkEventThread(_connection.Servers);
                _eventThread.Start();
                _connection.Connect(watcher);

                Logger.Debug("Awaiting connection to Zookeeper server");
                if (!WaitUntilConnected(TimeSpan.FromMilliseconds(maxMsToWaitUntilConnected)))
                {
                    throw new ZkTimeoutException("Unable to connect to zookeeper server within timeout: " + maxMsToWaitUntilConnected);
                }

                started = true;
            }
            catch (ThreadInterruptedException)
            {
                ZooKeeper.States state = _connection.ZookeeperState;
                throw new Exception("Not connected with zookeeper server yet. Current state is " + state);
            }
            finally
            {
                EventLock.Unlock();

                // we should close the zookeeper instance, otherwise it would keep
                // on trying to connect
                if (!started)
                {
                    this.Dispose();
                }
            }
        }
Example #7
0
                /// <summary>
                /// 建立ZK连接 返回null表示连接不成功
                /// </summary>
                /// <param name="authEnum"></param>
                /// <param name="authInfo"></param>
                /// <returns></returns>
                public ZooKeeper Connect(AuthEnum authEnum, string authInfo)
        {
            try
            {
                foreach (string address in _address)
                {
                    _zooKeeper = new ZooKeeper(address, _sessiontTimeout, new NodeWatcher(_log));
                    if (authEnum != AuthEnum.world)
                    {
                        _zooKeeper.addAuthInfo(authEnum.ToString(), System.Text.Encoding.UTF8.GetBytes(authInfo));
                    }
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    while (stopwatch.ElapsedMilliseconds < _connectTimeout / _address.Count)
                    {
                        ZooKeeper.States states = _zooKeeper.getState();
                        if (states == ZooKeeper.States.CONNECTED || states == ZooKeeper.States.CONNECTEDREADONLY)
                        {
                            break;
                        }
                    }
                    stopwatch.Stop();
                    if (_zooKeeper.getState().ToString().ToUpper().Contains("CONNECTED"))
                    {
                        break;
                    }
                }

                return(_zooKeeper);
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("连接zookeeper发生异常:{0}", ex.Message + ex.StackTrace);
            }
            return(null);
        }
Example #8
0
        /**
         * Returns whether we are connected to a server (which
         * could possibly be read-only, if this client is allowed
         * to go to read-only mode)
         * */

        public static bool isConnected(this ZooKeeper.States state)
        {
            return(state == ZooKeeper.States.CONNECTED || state == ZooKeeper.States.CONNECTEDREADONLY);
        }
Example #9
0
 public static bool isAlive(this ZooKeeper.States state)
 {
     return(state != ZooKeeper.States.CLOSED && state != ZooKeeper.States.AUTH_FAILED);
 }