-
-
Save gdyrrahitis/cf48b58bd8fa06dc0ddbf373823ec526 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ProducerBase<T> : RabbitMqClientBase, IRabbitMqProducer<T> | |
{ | |
private readonly ILogger<ProducerBase<T>> _logger; | |
protected abstract string ExchangeName { get; } | |
protected abstract string RoutingKeyName { get; } | |
protected abstract string AppId { get; } | |
protected ProducerBase( | |
ConnectionFactory connectionFactory, | |
ILogger<RabbitMqClientBase> logger, | |
ILogger<ProducerBase<T>> producerBaseLogger) : | |
base(connectionFactory, logger) => _logger = producerBaseLogger; | |
public virtual void Publish(T @event) | |
{ | |
try | |
{ | |
var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event)); | |
var properties = Channel.CreateBasicProperties(); | |
properties.AppId = AppId; | |
properties.ContentType = "application/json"; | |
properties.DeliveryMode = 1; // Doesn't persist to disk | |
properties.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); | |
Channel.BasicPublish(exchange: ExchangeName, routingKey: RoutingKeyName, body: body, basicProperties: properties); | |
} | |
catch (Exception ex) | |
{ | |
_logger.LogCritical(ex, "Error while publishing"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment