public static void DeclareExchangeForConnection(this IModel channel, RmqMessagingGatewayConnection connection) { var arguments = new Dictionary <string, object>(); if (connection.Exchange.SupportDelay) { arguments.Add("x-delayed-type", connection.Exchange.Type); connection.Exchange.Type = "x-delayed-message"; } channel.ExchangeDeclare(connection.Exchange.Name, connection.Exchange.Type, connection.Exchange.Durable, autoDelete: false, arguments: arguments); }
public static void DeclareExchangeForConnection(this IModel channel, RmqMessagingGatewayConnection connection) { if (connection.Exchange.SupportDelay) { channel.ExchangeDeclare(connection.Exchange.Name, "x-delayed-message", connection.Exchange.Durable, autoDelete: false, arguments: new Dictionary <string, object> { { "x-delayed-type", connection.Exchange.Type } }); } else { channel.ExchangeDeclare(connection.Exchange.Name, connection.Exchange.Type, connection.Exchange.Durable); } }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessagePublisher"/> class. /// </summary> /// <param name="channel">The channel.</param> /// <param name="connection">The exchange we want to talk to.</param> /// <param name="makeChannel">Do we create the exchange, if it does not exist? Note that without a bound consumer, messages are discarded</param> /// <exception cref="System.ArgumentNullException"> /// channel /// or /// exchangeName /// </exception> public RmqMessagePublisher(IModel channel, RmqMessagingGatewayConnection connection) { if (channel is null) { throw new ArgumentNullException(nameof(channel)); } if (connection is null) { throw new ArgumentNullException(nameof(connection)); } _channel = channel; _connection = connection; }
/// <summary> /// Initializes a new instance of the <see cref="RMQMessageGateway" /> class. /// </summary> /// <param name="connection"></param> /// <param name="queueName">The queue name.</param> /// <param name="routingKeys">The routing keys.</param> /// <param name="isDurable">Is the queue persisted to disk</param> /// <param name="highAvailability"></param> public RmqMessageConsumer( RmqMessagingGatewayConnection connection, string queueName, string[] routingKeys, bool isDurable, bool highAvailability = false) : base(connection) { _queueName = queueName; _routingKeys = new RoutingKeys(routingKeys); _isDurable = isDurable; IsQueueMirroredAcrossAllNodesInTheCluster = highAvailability; _messageCreator = new RmqMessageCreator(); }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageGateway" /> class. /// </summary> /// <param name="connection"></param> /// <param name="queueName">The queue name.</param> /// <param name="routingKey">The routing key.</param> /// <param name="isDurable">Is the queue definition persisted</param> /// <param name="highAvailability">Is the queue available on all nodes in a cluster</param> /// <param name="batchSize">How many messages to retrieve at one time; ought to be size of channel buffer</param> /// <param name="deadLetterQueueName">The dead letter queue</param> /// <param name="deadLetterRoutingKey">The routing key for dead letter messages</param> /// <param name="ttl">How long before a message on the queue expires in milliseconds. Defaults to infinite</param> /// <param name="makeChannels">Should we validate, or create missing channels</param> public RmqMessageConsumer(RmqMessagingGatewayConnection connection, string queueName, string routingKey, bool isDurable, bool highAvailability = false, int batchSize = 1, string deadLetterQueueName = null, string deadLetterRoutingKey = null, int?ttl = null, int?maxQueueLength = null, OnMissingChannel makeChannels = OnMissingChannel.Create) : this(connection, queueName, new string[] { routingKey }, isDurable, highAvailability, batchSize, deadLetterQueueName, deadLetterRoutingKey, ttl, maxQueueLength, makeChannels) { }
/// <summary> /// Initializes a new instance of the <see cref="RMQMessageGateway"/> class. /// Use if you need to inject a test logger /// <param name="connection">The amqp uri and exchange to connect to</param> /// </summary> protected RMQMessageGateway(RmqMessagingGatewayConnection connection) { Connection = connection; var connectionPolicyFactory = new ConnectionPolicyFactory(Connection); _retryPolicy = connectionPolicyFactory.RetryPolicy; _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy; _connectionFactory = new ConnectionFactory { Uri = Connection.AmpqUri.Uri.ToString(), RequestedHeartbeat = 30 }; DelaySupported = Connection.Exchange.SupportDelay; }
/// <summary> /// Initializes a new instance of the <see cref="RMQMessageGateway" /> class. /// </summary> /// <param name="connection"></param> /// <param name="queueName">The queue name.</param> /// <param name="routingKey">The routing key.</param> /// <param name="isDurable">Is the queue persisted to disk</param> /// <param name="preFetchSize">0="Don't send me a new message until I?ve finished", 1= "Send me one message at a time", n = number to grab (take care with competing consumers)</param> /// <param name="highAvailability"></param> public RmqMessageConsumer( RmqMessagingGatewayConnection connection, string queueName, string routingKey, bool isDurable, ushort preFetchSize = 1, bool highAvailability = false) : base(connection) { _queueName = queueName; _routingKey = routingKey; _isDurable = isDurable; _preFetchSize = preFetchSize; IsQueueMirroredAcrossAllNodesInTheCluster = highAvailability; _messageCreator = new RmqMessageCreator(); }
private static void ValidateExchange(IModel channel, RmqMessagingGatewayConnection connection) { try { channel.ExchangeDeclarePassive(connection.Exchange.Name); if (connection.DeadLetterExchange != null) { channel.ExchangeDeclarePassive(connection.DeadLetterExchange.Name); } } catch (Exception e) { throw new BrokerUnreachableException(e); } }
public static void DeclareExchangeForConnection(this IModel channel, RmqMessagingGatewayConnection connection, OnMissingChannel onMissingChannel) { if (onMissingChannel == OnMissingChannel.Assume) { return; } if (onMissingChannel == OnMissingChannel.Create) { CreateExchange(channel, connection); } else if (onMissingChannel == OnMissingChannel.Validate) { ValidateExchange(channel, connection); } }
/// <summary> /// Initializes a new instance of the <see cref="RMQMessageGateway" /> class. /// </summary> /// <param name="connection"></param> /// <param name="queueName">The queue name.</param> /// <param name="routingKeys">The routing keys.</param> /// <param name="isDurable">Is the queue persisted to disk</param> /// <param name="highAvailability"></param> public RmqMessageConsumer( RmqMessagingGatewayConnection connection, string queueName, string[] routingKeys, bool isDurable, bool highAvailability = false, int batchSize = 1) : base(connection, batchSize) { _queueName = queueName; _routingKeys = new RoutingKeys(routingKeys); _isDurable = isDurable; IsQueueMirroredAcrossAllNodesInTheCluster = highAvailability; _messageCreator = new RmqMessageCreator(); _batchSize = batchSize; _consumerTag = Connection.Name + Guid.NewGuid(); }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageGateway" /> class. /// Use if you need to inject a test logger /// </summary> /// <param name="connection">The amqp uri and exchange to connect to</param> /// <param name="batchSize">How many messages to read from a channel at one time. Only used by consumer, defaults to 1</param> protected RmqMessageGateway(RmqMessagingGatewayConnection connection) { Connection = connection; var connectionPolicyFactory = new ConnectionPolicyFactory(Connection); _retryPolicy = connectionPolicyFactory.RetryPolicy; _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy; _connectionFactory = new ConnectionFactory { Uri = Connection.AmpqUri.Uri, RequestedHeartbeat = TimeSpan.FromSeconds(connection.Heartbeat) }; DelaySupported = Connection.Exchange.SupportDelay; }
/// <summary> /// Initializes a new instance of the <see cref="RMQMessageGateway" /> class. /// Use if you need to inject a test logger /// <param name="connection">The amqp uri and exchange to connect to</param> /// </summary> protected RMQMessageGateway(RmqMessagingGatewayConnection connection, int batchSize = 1) { Connection = connection; _batchSize = Convert.ToUInt16(batchSize); var connectionPolicyFactory = new ConnectionPolicyFactory(Connection); _retryPolicy = connectionPolicyFactory.RetryPolicy; _circuitBreakerPolicy = connectionPolicyFactory.CircuitBreakerPolicy; _connectionFactory = new ConnectionFactory { Uri = Connection.AmpqUri.Uri, RequestedHeartbeat = connection.Heartbeat }; DelaySupported = Connection.Exchange.SupportDelay; }
/// <summary> /// Initializes a new instance of the <see cref="ConnectionPolicyFactory"/> class. /// Use if you need to inject a test logger /// </summary> /// <param name="connection"></param> public ConnectionPolicyFactory(RmqMessagingGatewayConnection connection) { var retries = connection.AmpqUri.ConnectionRetryCount; var retryWaitInMilliseconds = connection.AmpqUri.RetryWaitInMilliseconds; var circuitBreakerTimeout = connection.AmpqUri.CircuitBreakTimeInMilliseconds; RetryPolicy = Policy .Handle <BrokerUnreachableException>() .Or <Exception>() .WaitAndRetry( retries, retryAttempt => TimeSpan.FromMilliseconds(retryWaitInMilliseconds * Math.Pow(2, retryAttempt)), (exception, timeSpan, context) => { if (exception is BrokerUnreachableException) { _logger.Value.WarnException( "RMQMessagingGateway: BrokerUnreachableException error on connecting to queue {0} exchange {1} on connection {2}. Will retry {3} times", exception, context["queueName"], connection.Exchange.Name, connection.AmpqUri.GetSanitizedUri(), retries); } else { _logger.Value.WarnException( "RMQMessagingGateway: Exception on connection to queue {0} via exchange {1} on connection {2}", exception, context["queueName"], connection.Exchange.Name, connection.AmpqUri.GetSanitizedUri()); throw exception; } }); CircuitBreakerPolicy = Policy .Handle <BrokerUnreachableException>() .CircuitBreaker(1, TimeSpan.FromMilliseconds(circuitBreakerTimeout)); }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageGateway" /> class. /// </summary> /// <param name="connection">The subscription information needed to talk to RMQ</param> /// <param name="publication">How should we configure this producer. If not provided use default behaviours: /// Make Channels = Create /// </param> public RmqMessageProducer(RmqMessagingGatewayConnection connection, Publication publication) : base(connection) { _publication = publication; }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageProducerFactory"/> class. /// </summary> /// <param name="connection"></param> public RmqMessageProducerFactory(RmqMessagingGatewayConnection connection) { _connection = connection; }
/// <summary> /// Initializes a new instance of the <see cref="RMQMessageGateway" /> class. /// </summary> /// <param name="connection">The connection information needed to talk to RMQ</param> public RmqMessageProducer(RmqMessagingGatewayConnection connection) : base(connection, 1) { }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageGateway" /> class. /// </summary> /// <param name="connection">The subscription information needed to talk to RMQ</param> /// Make Channels = Create /// </param> public RmqMessageProducer(RmqMessagingGatewayConnection connection) : this(connection, new RmqPublication { MakeChannels = OnMissingChannel.Create }) { }
/// <summary> /// Initializes a new instance of the <see cref="RmqMessageConsumerFactory"/> class. /// </summary> public RmqMessageConsumerFactory(RmqMessagingGatewayConnection connection) { _connection = connection; }