示例#1
0
        Task SendUsingNewConnection(IPipe <ConnectionContext> connectionPipe, ConnectionScope scope, CancellationToken cancellationToken)
        {
            try
            {
                if (_cacheTaskScope.StoppingToken.IsCancellationRequested)
                {
                    throw new TaskCanceledException($"The connection is being disconnected: {_settings.ToDebugString()}");
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connecting: {0}", _settings.ToDebugString());
                }

                IConnection connection;
                if (_settings.ClusterMembers?.Any() ?? false)
                {
                    connection = _connectionFactory.CreateConnection(_settings.ClusterMembers, _settings.Host);
                }
                else
                {
                    connection = _connectionFactory.CreateConnection();
                }


                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _settings.ToDebugString(),
                                     connection.Endpoint, connection.LocalPort);
                }

                EventHandler <ShutdownEventArgs> connectionShutdown = null;
                connectionShutdown = (obj, reason) =>
                {
                    Interlocked.CompareExchange(ref _scope, null, scope);

                    scope.Shutdown(reason.ReplyText);

                    connection.ConnectionShutdown -= connectionShutdown;
                };

                connection.ConnectionShutdown += connectionShutdown;

                var connectionContext = new RabbitMqConnectionContext(connection, _settings, _cacheTaskScope);

                connectionContext.GetOrAddPayload(() => _settings);

                scope.Connected(connectionContext);
            }
            catch (BrokerUnreachableException ex)
            {
                Interlocked.CompareExchange(ref _scope, null, scope);

                scope.ConnectFaulted(ex);

                throw new RabbitMqConnectionException("Connect failed: " + _settings.ToDebugString(), ex);
            }

            return(SendUsingExistingConnection(connectionPipe, scope, cancellationToken));
        }
示例#2
0
        async Task <ConnectionContext> CreateConnection(ISupervisor supervisor)
        {
            IConnection connection = null;

            try
            {
                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {_description}");
                }

                LogContext.Debug?.Log("Connecting: {Host}", _description);

                if (_configuration.Settings.ClusterMembers?.Any() ?? false)
                {
                    connection = _connectionFactory.Value.CreateConnection(_configuration.Settings.ClusterMembers, _configuration.Settings.ClientProvidedName);
                }
                else
                {
                    List <string> hostNames = Enumerable.Repeat(_configuration.Settings.Host, 1).ToList();

                    connection = _connectionFactory.Value.CreateConnection(hostNames, _configuration.Settings.ClientProvidedName);
                }

                LogContext.Debug?.Log("Connected: {Host} (address: {RemoteAddress}, local: {LocalAddress})", _description, connection.Endpoint,
                                      connection.LocalPort);

                var connectionContext = new RabbitMqConnectionContext(connection, _configuration, _hostTopology, _description, supervisor.Stopped);

                connectionContext.GetOrAddPayload(() => _configuration.Settings);

                return(connectionContext);
            }
            catch (ConnectFailureException ex)
            {
                connection?.Dispose();

                throw new RabbitMqConnectionException("Connect failed: " + _description, ex);
            }
            catch (BrokerUnreachableException ex)
            {
                connection?.Dispose();

                throw new RabbitMqConnectionException("Broker unreachable: " + _description, ex);
            }
            catch (OperationInterruptedException ex)
            {
                connection?.Dispose();

                throw new RabbitMqConnectionException("Operation interrupted: " + _description, ex);
            }
        }
示例#3
0
            public void Connected(RabbitMqConnectionContext connectionContext)
            {
                _connectionContext.TrySetResult(connectionContext);

                _taskScope.SetReady();
            }
示例#4
0
        async Task <ConnectionContext> CreateConnection(ISupervisor supervisor)
        {
            return(await _hostConfiguration.ReceiveTransportRetryPolicy.Retry(async() =>
            {
                var description = _hostConfiguration.Settings.ToDescription();

                if (supervisor.Stopping.IsCancellationRequested)
                {
                    throw new OperationCanceledException($"The connection is stopping and cannot be used: {description}");
                }

                IConnection connection = null;
                try
                {
                    TransportLogMessages.ConnectHost(description);

                    if (_hostConfiguration.Settings.EndpointResolver != null)
                    {
                        connection = _connectionFactory.Value.CreateConnection(_hostConfiguration.Settings.EndpointResolver,
                                                                               _hostConfiguration.Settings.ClientProvidedName);
                    }
                    else
                    {
                        var hostNames = new List <string>(1)
                        {
                            _hostConfiguration.Settings.Host
                        };

                        connection = _connectionFactory.Value.CreateConnection(hostNames, _hostConfiguration.Settings.ClientProvidedName);
                    }

                    LogContext.Debug?.Log("Connected: {Host} (address: {RemoteAddress}, local: {LocalAddress})", description, connection.Endpoint,
                                          connection.LocalPort);

                    var connectionContext = new RabbitMqConnectionContext(connection, _hostConfiguration, supervisor.Stopped);

                    connectionContext.GetOrAddPayload(() => _hostConfiguration.Settings);

                    return (ConnectionContext)connectionContext;
                }
                catch (ConnectFailureException ex)
                {
                    connection?.Dispose();

                    throw new RabbitMqConnectionException("Connect failed: " + description, ex);
                }
                catch (BrokerUnreachableException ex)
                {
                    connection?.Dispose();

                    throw new RabbitMqConnectionException("Broker unreachable: " + description, ex);
                }
                catch (OperationInterruptedException ex)
                {
                    connection?.Dispose();

                    throw new RabbitMqConnectionException("Operation interrupted: " + description, ex);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    connection?.Dispose();

                    throw new RabbitMqConnectionException("Create Connection Faulted: " + description, ex);
                }
            }, supervisor.Stopping).ConfigureAwait(false));
        }
        async Task SendUsingNewConnection(IPipe <ConnectionContext> connectionPipe, ConnectionScope scope, CancellationToken cancellationToken)
        {
            try
            {
                if (_signal.CancellationToken.IsCancellationRequested)
                {
                    throw new TaskCanceledException($"The connection is being disconnected: {_settings.ToDebugString()}");
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connecting: {0}", _connectionFactory.ToDebugString());
                }

                IConnection connection = _connectionFactory.CreateConnection();

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _connectionFactory.ToDebugString(),
                                     connection.RemoteEndPoint, connection.LocalEndPoint);
                }

                EventHandler <ShutdownEventArgs> connectionShutdown = null;
                connectionShutdown = (obj, reason) =>
                {
                    connection.ConnectionShutdown -= connectionShutdown;

                    Interlocked.CompareExchange(ref _scope, null, scope);

                    scope.Close();
                };

                connection.ConnectionShutdown += connectionShutdown;

                var connectionContext = new RabbitMqConnectionContext(connection, _settings, _signal.CancellationToken);

                connectionContext.GetOrAddPayload(() => _settings);

                scope.Connected(connectionContext);
            }
            catch (BrokerUnreachableException ex)
            {
                Interlocked.CompareExchange(ref _scope, null, scope);

                scope.ConnectFaulted(ex);

                throw new RabbitMqConnectionException("Connect failed: " + _connectionFactory.ToDebugString(), ex);
            }

            try
            {
                using (SharedConnectionContext context = await scope.Attach(cancellationToken))
                {
                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("Using new connection: {0}", ((ConnectionContext)context).HostSettings.ToDebugString());
                    }

                    await connectionPipe.Send(context);
                }
            }
            catch (Exception ex)
            {
                if (_log.IsDebugEnabled)
                {
                    _log.Debug("The connection usage threw an exception", ex);
                }

                throw;
            }
        }
 public void Connected(RabbitMqConnectionContext connectionContext)
 {
     _connectionContext.TrySetResult(connectionContext);
 }
示例#7
0
        Task SendUsingNewConnection(IPipe <ConnectionContext> connectionPipe, ConnectionScope scope, CancellationToken cancellationToken)
        {
            IConnection connection = null;

            try
            {
                if (_cacheTaskScope.StoppingToken.IsCancellationRequested)
                {
                    throw new TaskCanceledException($"The connection is being disconnected: {_description}");
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connecting: {0}", _description);
                }

                if (_settings.ClusterMembers?.Any() ?? false)
                {
                    connection = _connectionFactory.Value.CreateConnection(_settings.ClusterMembers, _settings.ClientProvidedName);
                }
                else
                {
                    List <string> hostNames = Enumerable.Repeat(_settings.Host, 1).ToList();

                    connection = _connectionFactory.Value.CreateConnection(hostNames, _settings.ClientProvidedName);
                }

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _description, connection.Endpoint, connection.LocalPort);
                }

                EventHandler <ShutdownEventArgs> connectionShutdown = null;
                connectionShutdown = (obj, reason) =>
                {
                    Interlocked.CompareExchange(ref _scope, null, scope);

                    scope.Shutdown(reason.ReplyText);

                    connection.ConnectionShutdown -= connectionShutdown;
                };

                connection.ConnectionShutdown += connectionShutdown;

                var connectionContext = new RabbitMqConnectionContext(connection, _settings, _topology, _description, _cacheTaskScope);

                connectionContext.GetOrAddPayload(() => _settings);

                scope.Connected(connectionContext);
            }
            catch (BrokerUnreachableException ex)
            {
                if (_log.IsDebugEnabled)
                {
                    _log.Debug("The broker was unreachable", ex);
                }

                Interlocked.CompareExchange(ref _scope, null, scope);

                scope.ConnectFaulted(ex);

                connection?.Dispose();

                throw new RabbitMqConnectionException("Connect failed: " + _description, ex);
            }
            catch (OperationInterruptedException ex)
            {
                if (_log.IsDebugEnabled)
                {
                    _log.Debug("The RabbitMQ operation was interrupted", ex);
                }

                Interlocked.CompareExchange(ref _scope, null, scope);

                scope.ConnectFaulted(ex);

                connection?.Dispose();

                throw new RabbitMqConnectionException("Operation interrupted: " + _description, ex);
            }

            return(SendUsingExistingConnection(connectionPipe, scope, cancellationToken));
        }
        async Task SendUsingNewConnection(IPipe<ConnectionContext> connectionPipe, ConnectionScope scope, CancellationToken cancellationToken)
        {
            try
            {
                if (_signal.CancellationToken.IsCancellationRequested)
                    throw new TaskCanceledException($"The connection is being disconnected: {_settings.ToDebugString()}");

                if (_log.IsDebugEnabled)
                    _log.DebugFormat("Connecting: {0}", _connectionFactory.ToDebugString());

                IConnection connection = _connectionFactory.CreateConnection();

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _connectionFactory.ToDebugString(),
                        connection.RemoteEndPoint, connection.LocalEndPoint);
                }

                EventHandler<ShutdownEventArgs> connectionShutdown = null;
                connectionShutdown = (obj, reason) =>
                {
                    connection.ConnectionShutdown -= connectionShutdown;

                    Interlocked.CompareExchange(ref _scope, null, scope);

                    scope.Close();
                };

                connection.ConnectionShutdown += connectionShutdown;

                var connectionContext = new RabbitMqConnectionContext(connection, _settings, _signal.CancellationToken);

                connectionContext.GetOrAddPayload(() => _settings);

                scope.Connected(connectionContext);
            }
            catch (BrokerUnreachableException ex)
            {
                Interlocked.CompareExchange(ref _scope, null, scope);

                scope.ConnectFaulted(ex);

                throw new RabbitMqConnectionException("Connect failed: " + _connectionFactory.ToDebugString(), ex);
            }

            try
            {
                using (SharedConnectionContext context = await scope.Attach(cancellationToken))
                {
                    if (_log.IsDebugEnabled)
                        _log.DebugFormat("Using new connection: {0}", ((ConnectionContext)context).HostSettings.ToDebugString());

                    await connectionPipe.Send(context);
                }
            }
            catch (Exception ex)
            {
                if (_log.IsDebugEnabled)
                    _log.Debug("The connection usage threw an exception", ex);

                throw;
            }
        }
 public void Connected(RabbitMqConnectionContext connectionContext)
 {
     _connectionContext.TrySetResult(connectionContext);
 }
        Task SendUsingNewConnection(IPipe<ConnectionContext> connectionPipe, ConnectionScope scope, CancellationToken cancellationToken)
        {
            try
            {
                if (_cacheTaskScope.StoppingToken.IsCancellationRequested)
                    throw new TaskCanceledException($"The connection is being disconnected: {_settings.ToDebugString()}");

                if (_log.IsDebugEnabled)
                    _log.DebugFormat("Connecting: {0}", _settings.ToDebugString());

                IConnection connection;
                if (_settings.ClusterMembers?.Any() ?? false)
                {
                    connection = _connectionFactory.CreateConnection(_settings.ClusterMembers, _settings.Host);
                }
                else
                {
                    connection = _connectionFactory.CreateConnection();
                }


                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Connected: {0} (address: {1}, local: {2}", _settings.ToDebugString(),
                        connection.Endpoint, connection.LocalPort);
                }

                EventHandler<ShutdownEventArgs> connectionShutdown = null;
                connectionShutdown = (obj, reason) =>
                {
                    Interlocked.CompareExchange(ref _scope, null, scope);

                    scope.Shutdown(reason.ReplyText);

                    connection.ConnectionShutdown -= connectionShutdown;
                };

                connection.ConnectionShutdown += connectionShutdown;

                var connectionContext = new RabbitMqConnectionContext(connection, _settings, _cacheTaskScope);

                connectionContext.GetOrAddPayload(() => _settings);

                scope.Connected(connectionContext);
            }
            catch (BrokerUnreachableException ex)
            {
                Interlocked.CompareExchange(ref _scope, null, scope);

                scope.ConnectFaulted(ex);

                throw new RabbitMqConnectionException("Connect failed: " + _settings.ToDebugString(), ex);
            }

            return SendUsingExistingConnection(connectionPipe, scope, cancellationToken);
        }
            public void Connected(RabbitMqConnectionContext connectionContext)
            {
                _connectionContext.TrySetResult(connectionContext);

                _taskScope.SetReady();
            }