public void Send(object message, IConventions conventions, string messageId = null, string correlationId = null, object correlationContext = null) { var json = _serializer.Serialize(message); var body = Encoding.UTF8.GetBytes(json); var properties = _channel.CreateBasicProperties(); properties.MessageId = string.IsNullOrWhiteSpace(messageId) ? Guid.NewGuid().ToString("N") : messageId; properties.CorrelationId = string.IsNullOrWhiteSpace(correlationId) ? Guid.NewGuid().ToString("N") : correlationId; properties.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); properties.Headers = new Dictionary <string, object>(); if (_contextEnabled) { IncludeCorrelationContext(correlationContext, properties); } if (_loggerEnabled) { _logger.LogInformation($"Publishing a message with routing key: '{conventions.RoutingKey}' " + $"to exchange: '{conventions.Exchange}' " + $"[id: '{properties.MessageId}', correlation id: '{properties.CorrelationId}']"); } _channel.BasicPublish(conventions.Exchange, conventions.RoutingKey, properties, body); }
public void Send(object message, IConventions conventions, string messageId = null, string correlationId = null, string spanContext = null, object messageContext = null, IDictionary <string, object> headers = null) { var payload = _serializer.Serialize(message); var body = Encoding.UTF8.GetBytes(payload); var properties = _channel.CreateBasicProperties(); properties.MessageId = string.IsNullOrWhiteSpace(messageId) ? Guid.NewGuid().ToString("N") : messageId; properties.CorrelationId = string.IsNullOrWhiteSpace(correlationId) ? Guid.NewGuid().ToString("N") : correlationId; properties.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); properties.Headers = new Dictionary <string, object>(); if (_contextEnabled) { IncludeMessageContext(messageContext, properties); } if (!string.IsNullOrWhiteSpace(spanContext)) { properties.Headers.Add(_spanContextHeader, spanContext); } if (headers is {})
/// <summary> /// Publish the <paramref name="message"/> but deliver it later at the moment <paramref name="deliverAt"/> /// </summary> /// <remarks> /// The message will be delivered not early than <paramref name="deliverAt"/>. /// Published message will be stored in the intermediate storage until the delivery moment comes. /// If the method is executed without exception, it is guaranteed that the message will be eventually delivered at least once /// </remarks> /// <param name="message">Message to publish</param> /// <param name="deliverAt">Moment, when message should be delivered</param> /// <param name="routingKey">Message routing key. Overrides routing key, which specified in the publisher settings</param> /// <returns></returns> public Task ProduceAsync(TMessageModel message, DateTime deliverAt, string routingKey = null) { ThrowIfNotStarted(); if (message == null) { throw new ArgumentNullException(nameof(message)); } if (_deferredMessagesManager == null) { throw new InvalidOperationException($"To enable deferred messages publishing, call {nameof(EnableDeferredMessages)}"); } var body = _serializer.Serialize(message); return(_deferredMessagesManager.DeferAsync(new RawMessage(body, routingKey), deliverAt)); }
public void Send(object message, IConventions conventions, string messageId = null, string correlationId = null, string spanContext = null, object messageContext = null, IDictionary <string, object> headers = null) { var threadId = Thread.CurrentThread.ManagedThreadId; if (!_channels.TryGetValue(threadId, out var channel)) { lock (_lockObject) { if (_channelsCount >= _maxChannels) { throw new InvalidOperationException($"Cannot create RabbitMQ producer channel for thread: {threadId} " + $"(reached the limit of {_maxChannels} channels). " + "Modify `MaxProducerChannels` setting to allow more channels."); } channel = _connection.CreateModel(); _channels.TryAdd(threadId, channel); _channelsCount++; if (_loggerEnabled) { _logger.LogTrace($"Created a channel for thread: {threadId}, total channels: {_channelsCount}/{_maxChannels}"); } } } else { if (_loggerEnabled) { _logger.LogTrace($"Reused a channel for thread: {threadId}, total channels: {_channelsCount}/{_maxChannels}"); } } var payload = _serializer.Serialize(message); var body = Encoding.UTF8.GetBytes(payload); var properties = channel.CreateBasicProperties(); properties.Persistent = _persistMessages; properties.MessageId = string.IsNullOrWhiteSpace(messageId) ? Guid.NewGuid().ToString("N") : messageId; properties.CorrelationId = string.IsNullOrWhiteSpace(correlationId) ? Guid.NewGuid().ToString("N") : correlationId; properties.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); properties.Headers = new Dictionary <string, object>(); if (_contextEnabled) { IncludeMessageContext(messageContext, properties); } if (!string.IsNullOrWhiteSpace(spanContext)) { properties.Headers.Add(_spanContextHeader, spanContext); } if (headers is {})
public Task Send(object message, IConventions conventions, string messageId = null, string correlationId = null, string spanContext = null, object messageContext = null, IDictionary <string, object> headers = null) { var payload = _serializer.Serialize(message); var body = Encoding.UTF8.GetBytes(payload); IBasicProperties properties = CreateProperties(messageId, correlationId, headers); _channel.BasicPublish(conventions.Exchange, conventions.RoutingKey, properties, body); return(Task.CompletedTask); }
public void Publish(string message) { QueueDeclareConfiguration QueueConfiguration = new QueueDeclareConfiguration(ChannelQueueName); QueueConfiguration.Declare(ChannelAccessor.Channel); var properties = ChannelAccessor.Channel.CreateBasicProperties(); properties.Persistent = true; ChannelAccessor.Channel.BasicPublish( exchange: "", routingKey: QueueConfiguration.QueueName, basicProperties: properties, body: serializer.Serialize(string.Format(message, DateTime.Now)) ); _logger.LogInformation(" [RabbitBasicDemo] Publish {0}", string.Format(message, DateTime.Now)); }
private byte[] RepackMessage(byte[] serializedMessage) { T message; try { message = _messageDeserializer.Deserialize(serializedMessage); } catch (Exception exception) { _log.WriteErrorAsync(this.GetType().Name, nameof(RepackMessage), $"Failed to deserialize the message: {serializedMessage} with {_messageDeserializer.GetType().Name}. Stopping.", exception).GetAwaiter().GetResult(); return(null); } return(_messageSerializer.Serialize(message)); }
public Task InvokeAsync(HttpContext context, RequestDelegate next) { QueueDeclareConfiguration QueueConfiguration = new QueueDeclareConfiguration(ChannelQueueName); QueueConfiguration.Declare(ChannelAccessor.Channel); var properties = ChannelAccessor.Channel.CreateBasicProperties(); properties.Persistent = true; ChannelAccessor.Channel.BasicPublish( exchange: "", routingKey: QueueConfiguration.QueueName, basicProperties: properties, body: serializer.Serialize(string.Format(message, DateTime.Now)) ); Console.WriteLine(" [Publish] Publish {0}", string.Format(message, DateTime.Now)); return(next.Invoke(context)); }