Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis
Last active May 23, 2020 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdyrrahitis/cf48b58bd8fa06dc0ddbf373823ec526 to your computer and use it in GitHub Desktop.
Save gdyrrahitis/cf48b58bd8fa06dc0ddbf373823ec526 to your computer and use it in GitHub Desktop.
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