/// <summary> /// Requeues the specified message. /// </summary> /// <param name="message"></param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> public void Requeue(Message message, int delayMilliseconds) { try { _logger.Value.DebugFormat("RmqMessageConsumer: Re-queueing message {0} with a delay of {1} milliseconds", message.Id, delayMilliseconds); EnsureChannel(_queueName); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection.Exchange.Name); if (DelaySupported) { rmqMessagePublisher.RequeueMessage(message, _queueName, delayMilliseconds); } else { Task.Delay(delayMilliseconds).Wait(); rmqMessagePublisher.RequeueMessage(message, _queueName, 0); } Reject(message, false); } catch (Exception exception) { _logger.Value.ErrorException("RmqMessageConsumer: Error re-queueing message {0}", exception, message.Id); throw; } }
/// <summary> /// Requeues the specified message. /// </summary> /// <param name="message"></param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> public void Requeue(Message message, int delayMilliseconds) { try { _logger.Value.DebugFormat("RmqMessageConsumer: Re-queueing message {0} with a delay of {1} milliseconds", message.Id, delayMilliseconds); EnsureBroker(_queueName); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection); if (DelaySupported) { rmqMessagePublisher.RequeueMessage(message, _queueName, delayMilliseconds); } else { Task.Delay(delayMilliseconds).Wait(); rmqMessagePublisher.RequeueMessage(message, _queueName, 0); } //Delete it if we have re-queued it - note this will forward the deleted message to a DLQ if there is one as we choose to republish a requeue not just //make it available for a new consumer Channel.BasicNack(message.DeliveryTag, false, false); } catch (Exception exception) { _logger.Value.ErrorException("RmqMessageConsumer: Error re-queueing message {0}", exception, message.Id); throw; } }
/// <summary> /// Requeues the specified message. /// </summary> /// <param name="message"></param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> /// <returns>True if message deleted, false otherwise</returns> public bool Requeue(Message message, int delayMilliseconds) { try { s_logger.LogDebug("RmqMessageConsumer: Re-queueing message {Id} with a delay of {Delay} milliseconds", message.Id, delayMilliseconds); EnsureBroker(_queueName); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection); if (DelaySupported) { rmqMessagePublisher.RequeueMessage(message, _queueName, delayMilliseconds); } else { Task.Delay(delayMilliseconds).Wait(); rmqMessagePublisher.RequeueMessage(message, _queueName, 0); } //ack the original message to remove it from the queue var deliveryTag = message.DeliveryTag; s_logger.LogInformation("RmqMessageConsumer: Deleting message {Id} with delivery tag {DeliveryTag} as re-queued", message.Id, deliveryTag); Channel.BasicAck(deliveryTag, false); return(true); } catch (Exception exception) { s_logger.LogError(exception, "RmqMessageConsumer: Error re-queueing message {Id}", message.Id); return(false); } }
/// <summary> /// Send the specified message with specified delay /// </summary> /// <param name="message">The message.</param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> /// <returns>Task.</returns> public void SendWithDelay(Message message, int delayMilliseconds = 0) { lock (_lock) { _logger.Value.DebugFormat("RmqMessageProducer: Preparing to send message via exchange {0}", Connection.Exchange.Name); EnsureChannel(); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection.Exchange.Name); _logger.Value.DebugFormat("RmqMessageProducer: Publishing message to exchange {0} on connection {1} with a delay of {5} and topic {2} and id {3} and body: {4}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), message.Header.Topic, message.Id, message.Body.Value, delayMilliseconds); rmqMessagePublisher.PublishMessage(message, delayMilliseconds); _logger.Value.InfoFormat("RmqMessageProducer: Published message to exchange {0} on connection {1} with a delay of {5} and topic {2} and id {3} and message: {4} at {5}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), message.Header.Topic, message.Id, JsonConvert.SerializeObject(message), DateTime.UtcNow, delayMilliseconds); } }
/// <summary> /// Send the specified message with specified delay /// </summary> /// <param name="message">The message.</param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> /// <returns>Task.</returns> public void SendWithDelay(Message message, int delayMilliseconds = 0) { try { lock (_lock) { s_logger.LogDebug("RmqMessageProducer: Preparing to send message via exchange {ExchangeName}", Connection.Exchange.Name); EnsureBroker(makeExchange: _publication.MakeChannels); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection); message.Persist = Connection.PersistMessages; Channel.BasicAcks += OnPublishSucceeded; Channel.BasicNacks += OnPublishFailed; Channel.ConfirmSelect(); _confirmsSelected = true; s_logger.LogDebug( "RmqMessageProducer: Publishing message to exchange {ExchangeName} on subscription {URL} with a delay of {Delay} and topic {Topic} and persisted {Persist} and id {Id} and body: {Request}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), delayMilliseconds, message.Header.Topic, message.Persist, message.Id, message.Body.Value); _pendingConfirmations.TryAdd(Channel.NextPublishSeqNo, message.Id); if (DelaySupported) { rmqMessagePublisher.PublishMessage(message, delayMilliseconds); } else { Task.Delay(delayMilliseconds).Wait(); rmqMessagePublisher.PublishMessage(message, 0); } s_logger.LogInformation( "RmqMessageProducer: Published message to exchange {ExchangeName} on subscription {URL} with a delay of {Delay} and topic {Topic} and persisted {Persist} and id {Id} and message: {Request} at {Time}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), delayMilliseconds, message.Header.Topic, message.Persist, message.Id, JsonSerializer.Serialize(message, JsonSerialisationOptions.Options), DateTime.UtcNow); } } catch (IOException io) { s_logger.LogError(io, "RmqMessageProducer: Error talking to the socket on {URL}, resetting subscription", Connection.AmpqUri.GetSanitizedUri() ); ResetConnectionToBroker(); throw new ChannelFailureException("Error talking to the broker, see inner exception for details", io); } }
/// <summary> /// Send the specified message with specified delay /// </summary> /// <param name="message">The message.</param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> /// <returns>Task.</returns> public void SendWithDelay(Message message, int delayMilliseconds = 0) { try { lock (_lock) { s_logger.LogDebug("RmqMessageProducer: Preparing to send message via exchange {0}", Connection.Exchange.Name); EnsureBroker(makeExchange: _publication.MakeChannels); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection); message.Persist = Connection.PersistMessages; s_logger.LogDebug( "RmqMessageProducer: Publishing message to exchange {0} on subscription {1} with a delay of {5} and topic {2} and persisted {6} and id {3} and body: {4}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), message.Header.Topic, message.Id, message.Body.Value, delayMilliseconds, message.Persist); if (DelaySupported) { rmqMessagePublisher.PublishMessage(message, delayMilliseconds); } else { Task.Delay(delayMilliseconds).Wait(); rmqMessagePublisher.PublishMessage(message, 0); } s_logger.LogInformation( "RmqMessageProducer: Published message to exchange {0} on subscription {1} with a delay of {6} and topic {2} and persisted {7} and id {3} and message: {4} at {5}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), message.Header.Topic, message.Id, JsonSerializer.Serialize(message, JsonSerialisationOptions.Options), DateTime.UtcNow, delayMilliseconds, message.Persist); } } catch (IOException io) { s_logger.LogError(io, "RmqMessageProducer: Error talking to the socket on {0}, resetting subscription", Connection.AmpqUri.GetSanitizedUri() ); ResetConnectionToBroker(); throw new ChannelFailureException("Error talking to the broker, see inner exception for details", io); } }
/// <summary> /// Send the specified message with specified delay /// </summary> /// <param name="message">The message.</param> /// <param name="delayMilliseconds">Number of milliseconds to delay delivery of the message.</param> /// <returns>Task.</returns> public void SendWithDelay(Message message, int delayMilliseconds = 0) { try { lock (_lock) { _logger.Value.DebugFormat("RmqMessageProducer: Preparing to send message via exchange {0}", Connection.Exchange.Name); EnsureChannel(); var rmqMessagePublisher = new RmqMessagePublisher(Channel, Connection.Exchange.Name); _logger.Value.DebugFormat( "RmqMessageProducer: Publishing message to exchange {0} on connection {1} with a delay of {5} and topic {2} and id {3} and body: {4}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), message.Header.Topic, message.Id, message.Body.Value, delayMilliseconds); if (DelaySupported) { rmqMessagePublisher.PublishMessage(message, delayMilliseconds); } else { Task.Delay(delayMilliseconds).Wait(); rmqMessagePublisher.PublishMessage(message, 0); } _logger.Value.InfoFormat( "RmqMessageProducer: Published message to exchange {0} on connection {1} with a delay of {5} and topic {2} and id {3} and message: {4} at {5}", Connection.Exchange.Name, Connection.AmpqUri.GetSanitizedUri(), message.Header.Topic, message.Id, JsonConvert.SerializeObject(message), DateTime.UtcNow, delayMilliseconds); } } catch (IOException io) { _logger.Value.ErrorFormat( "RmqMessageProducer: Error talking to the socket on {0}, resetting connection", Connection.AmpqUri.GetSanitizedUri() ); ResetConnectionToBroker(); throw new ChannelFailureException("Error talking to the broker, see inner exception for details", io); } }