diff --git a/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj b/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj index b0de771..834407f 100644 --- a/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj +++ b/src/Cap.Consistency.RabbitMQ/Cap.Consistency.RabbitMQ.csproj @@ -13,4 +13,8 @@ + + + + \ No newline at end of file diff --git a/src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs b/src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs new file mode 100644 index 0000000..86e0b34 --- /dev/null +++ b/src/Cap.Consistency.RabbitMQ/RabbitMQProducerClient.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Cap.Consistency.Infrastructure; +using Cap.Consistency.Producer; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using RabbitMQ.Client; + +namespace Cap.Consistency.RabbitMQ +{ + public class RabbitMQProducerClient : IProducerClient + { + + private readonly ConsistencyOptions _options; + private readonly ILogger _logger; + + public RabbitMQProducerClient(IOptions options, ILoggerFactory loggerFactory) { + _options = options.Value; + _logger = loggerFactory.CreateLogger(nameof(RabbitMQProducerClient)); + } + + public Task SendAsync(string topic, string content) { + var factory = new ConnectionFactory() { HostName = _options.BrokerUrlList }; + using (var connection = factory.CreateConnection()) + using (var channel = connection.CreateModel()) { + channel.ExchangeDeclare(exchange: "topic_logs", + type: "topic"); + + + var body = Encoding.UTF8.GetBytes(content); + channel.BasicPublish(exchange: "topic_logs", + routingKey: topic, + basicProperties: null, + body: body); + + return Task.CompletedTask; + } + } + } +}