Exemplo n.º 1
0
        private void Connect()
        {
            if (connection == null || !connection.IsOpen)
            {
                lock (syncLock)
                {
                    if (connection == null || !connection.IsOpen)
                    {
                        if (connection != null)
                        {
                            try
                            {
                                connection.Dispose();
                            }
                            catch
                            {
                                if (connection.CloseReason != null)
                                {
                                    this.logger.InfoWrite("Connection '{0}' has shutdown. Reason: '{1}'",
                                                          connection, connection.CloseReason.Cause);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }

                        connection = connectionFactory.CreateConnection();
                    }
                }
            }
        }
 /// <summary>
 /// Connects to RabbitMQ.
 /// </summary>
 private void Connect()
 {
     if (_connection == null || !_connection.IsOpen)
     {
         _connection = _connectionFactory.CreateConnection();
     }
 }
Exemplo n.º 3
0
        private void Connect()
        {
            if (_connection != null && _connection.IsOpen)
            {
                return;
            }
            object syncLock  = this._syncLock;
            bool   lockTaken = false;

            try
            {
                Monitor.Enter(syncLock, ref lockTaken);
                if (_connection != null && _connection.IsOpen || (_disposing || _disposed))
                {
                    return;
                }
                if (_connection != null)
                {
                    try
                    {
                        _connection.Dispose();
                    }
                    catch
                    {
                        if (_connection.CloseReason != null)
                        {
                            _logger.LogInformation($"Connection {_connection} has shutdown with reason={_connection.CloseReason.Cause}");
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                _connection = _connectionFactory.CreateConnection();
                if (!_disposing && !_disposed)
                {
                    return;
                }
                _connection.Dispose();
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(syncLock);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 连接RabbitMQ客户端
        /// </summary>
        protected void Connect()
        {
            if (connection == null || !connection.IsOpen)
            {
                lock (syncLock)
                {
                    if ((connection == null || !connection.IsOpen) && !(disposing || disposed))
                    {
                        if (connection != null)
                        {
                            try
                            {
                                connection.Dispose();
                            }
                            catch
                            {
                                if (connection.CloseReason != null)
                                {
                                    _logger.LogInformation("Connection {connection} has shutdown with reason={reason}", connection.ToString(), connection.CloseReason.Cause);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }

                        connection = connectionFactory.CreateConnection();
                        if (disposing || disposed)
                        {
                            connection.Dispose();
                        }
                    }
                }
            }
        }