Esempio n. 1
0
        /// <summary>
        /// 尝试连接到rabbitmq服务器,如果连接失败,则重试。
        /// </summary>
        public void Connect()
        {
            Monitor.Enter(ManagedConnectionFactory.SyncConnection);

            try
            {
                if (IsConnected || _retryPolicy.IsWaiting)
                {
                    return;
                }

                _watcher.DebugFormat("试图连接到端点:{0}", ConnectionFactory.Endpoint);
                var newConnection = ConnectionFactory.CreateConnection();
                newConnection.ConnectionShutdown += NewConnection_ConnectionShutdown;

                _retryPolicy.Reset();
                _watcher.InfoFormat("连接到RabbitMQ:Broker: {0}, VHost: {1}", ConnectionFactory.Endpoint, ConnectionFactory.HostName);
            }
            catch (ConnectFailureException connectFailureException)
            {
                HandleConnectionException(connectFailureException);
            }
            catch (BrokerUnreachableException brokerUnreachableException)
            {
                HandleConnectionException(brokerUnreachableException);
            }
            finally
            {
                Monitor.Exit(ManagedConnectionFactory.SyncConnection);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Try to connect to rabbitmq server, retry if it cann't connect to the broker.
        /// </summary>
        public virtual void Connect()
        {
            try
            {
                Monitor.Enter(ManagedConnectionFactory.SyncConnection);
                {
                    if (IsConnected || _retryPolicy.IsWaiting)
                    {
                        return;
                    }

                    _watcher.DebugFormat("Trying to connect to endpoint: '{0}'", ConnectionFactory.Endpoint);
                    var newConnection = ConnectionFactory.CreateConnection();
                    newConnection.ConnectionShutdown += SharedConnectionShutdown;

                    FireConnectedEvent();
                    _retryPolicy.Reset();
                    _watcher.InfoFormat("Connected to RabbitMQ. Broker: '{0}', VHost: '{1}'", ConnectionFactory.Endpoint, ConnectionFactory.VirtualHost);
                }
            }
            catch (ConnectFailureException connectFailureException)
            {
                HandleConnectionException(connectFailureException);
            }
            catch (BrokerUnreachableException brokerUnreachableException)
            {
                HandleConnectionException(brokerUnreachableException);
            }
            finally
            {
                Monitor.Exit(ManagedConnectionFactory.SyncConnection);
            }
        }
Esempio n. 3
0
        public void Connect()
        {
            try
            {
                lock (_syncConnection)
                {
                    if (IsConnected || _retryPolicy.IsWaiting)
                    {
                        return;
                    }

                    _watcher.DebugFormat("Trying to connect to endpoint: {0}", ConnectionFactory.Endpoint);
                    var newConnection = ConnectionFactory.CreateConnection();
                    newConnection.ConnectionShutdown += SharedConnectionShutdown;
                    //Console.WriteLine(ConnectionFactory.Endpoint + ConnectionFactory.VirtualHost);
                    SharedConnections.Add(ConnectionFactory.Endpoint + ConnectionFactory.VirtualHost, newConnection);
                    if (Connected != null)
                    {
                        Connected();
                    }

                    _retryPolicy.Reset();
                    _watcher.InfoFormat("Connected to RabbitMQ. Broker: '{0}', VHost: '{1}'", ConnectionFactory.Endpoint, ConnectionFactory.VirtualHost);
                }
            }
            catch (BrokerUnreachableException brokerUnreachableException)
            {
                _watcher.ErrorFormat("Failed to connect to Broker: '{0}', VHost: '{1}'. Retrying in {2} ms\n" +
                                     "Check HostName, VirtualHost, Username and Password.\n" +
                                     "ExceptionMessage: {3}",
                                     ConnectionFactory.HostName,
                                     ConnectionFactory.VirtualHost,
                                     _retryPolicy.DelayTime,
                                     brokerUnreachableException.Message);

                _retryPolicy.WaitForNextRetry(Connect);
            }
        }