Skip to content

Instantly share code, notes, and snippets.

@changhuixu
Last active June 2, 2020 12:57
Show Gist options
  • Select an option

  • Save changhuixu/bdef9103368409ecffb92ad700d74ccc to your computer and use it in GitHub Desktop.

Select an option

Save changhuixu/bdef9103368409ecffb92ad700d74ccc to your computer and use it in GitHub Desktop.
public class RabbitMQClient : IRabbitMQClient
{
private readonly IConnection _connection;
private readonly IModel _channel;
public RabbitMQClient(IConnection connection)
{
_connection = connection;
_channel = _connection.CreateModel();
_channel.ConfirmSelect();
}
public void Publish(string exchange, string routingKey, string payload)
{
var props = _channel.CreateBasicProperties();
props.AppId = "OrderApi";
props.Persistent = true;
props.UserId = "ops0";
props.MessageId = Guid.NewGuid().ToString("N");
props.Timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
var body = Encoding.UTF8.GetBytes(payload);
_channel.BasicPublish(exchange, routingKey, props, body);
_channel.WaitForConfirmsOrDie(new TimeSpan(0, 0, 5));
}
public void CloseConnection()
{
_connection?.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment