Last active
June 2, 2020 12:57
-
-
Save changhuixu/bdef9103368409ecffb92ad700d74ccc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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