Example #1
0
 /// <summary>
 ///     Re-connect to ZooKeeper server when session expired
 /// </summary>
 /// <param name="servers">
 ///     The servers.
 /// </param>
 /// <param name="connectionTimeout">
 ///     The connection timeout.
 /// </param>
 public void Reconnect(string servers, int connectionTimeout)
 {
     EnsuresNotDisposed();
     Logger.Debug("Reconnecting");
     connection.Dispose();
     connection = new ZooKeeperConnection(servers, connectionTimeout);
     connection.Connect(this);
     Logger.Debug("Reconnected");
 }
Example #2
0
        /// <summary>
        ///     Connects to ZooKeeper server within given time period and installs watcher in ZooKeeper
        /// </summary>
        /// <remarks>
        ///     Also, starts background thread for event handling
        /// </remarks>
        public void Connect()
        {
            EnsuresNotDisposed();
            var started = false;

            try
            {
                Logger.Info("Enter connect ...");
                shutdownTriggered = false;
                eventWorker       = new Thread(RunEventWorker)
                {
                    IsBackground = true
                };
                eventWorker.Name = "ZooKeeperkWatcher-EventThread-" + eventWorker.ManagedThreadId + "-" +
                                   connection.Servers;
                eventWorker.Start();
                Logger.Info("Will connect ...");
                connection.Connect(this);
                Logger.Info("Finish connect ...");
                Logger.Debug("Awaiting connection to Zookeeper server");
                if (!WaitUntilConnected(connectionTimeout))
                {
                    throw new ZooKeeperException(
                              "Unable to connect to zookeeper server within timeout: " + connectionTimeout);
                }
                started = true;
                Logger.Debug("Connection to Zookeeper server established");
            }
            catch (ThreadInterruptedException)
            {
                throw new InvalidOperationException(
                          "Not connected with zookeeper server yet. Current state is " + connection.ClientState);
            }
            finally
            {
                if (!started)
                {
                    Disconnect();
                }
            }
        }