implemented rabbitmq
This commit is contained in:
1 parent
7384cfcc84
commit
bc38afcbf8
5 files changed
+50
-4
No files matched your search
@@ -0,0 +1,32 @@
|
||||
using api.DTOs;
|
||||
using RabbitMQ.Client;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace api.RabbitMQ;
|
||||
|
||||
public class RabbitMQProducer : IMessageProducer
|
||||
{
|
||||
private IChannel? channel;
|
||||
private readonly string personQueueName = "persons";
|
||||
public async void SendMessage<T>(T message)
|
||||
{
|
||||
if (channel == null)
|
||||
{
|
||||
ConnectionFactory factory = new ConnectionFactory
|
||||
{
|
||||
Uri = new Uri("amqp://guest:guest@localhost:5672")
|
||||
};
|
||||
|
||||
IConnection conn = await factory.CreateConnectionAsync();
|
||||
channel = await conn.CreateChannelAsync();
|
||||
await channel.QueueDeclareAsync(personQueueName);
|
||||
}
|
||||
if (message is PersonDto)
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(message);
|
||||
byte[] body = Encoding.UTF8.GetBytes(jsonString);
|
||||
_ = channel.BasicPublishAsync(exchange: "", routingKey: personQueueName, body: body);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user