/// <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) { 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) { _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); } }