Skip to content

Commit

Permalink
the connection for consumer is defined here.
Browse files Browse the repository at this point in the history
  • Loading branch information
byerlikaya committed Jun 27, 2024
1 parent 4976291 commit f243d6d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Basic.RabbitMQ/MessageConsumer.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
namespace Basic.RabbitMQ;

public class MessageConsumer(RabbitMqClientService rabbitMqClientService) : IMessageConsumer
public class MessageConsumer(
RabbitMqClientService rabbitMqClientService,
ConnectionFactory connectionFactory) : IMessageConsumer, IDisposable
{
private IConnection _connection;

public IModel Channel(string queueName, string routingKey, ushort prefetchCount = 1)
{
var channel = rabbitMqClientService.Connect(queueName);
_connection ??= connectionFactory.CreateConnection();

var channel = rabbitMqClientService.Connect(_connection, queueName);
channel.QueueBind(
queue: queueName,
exchange: rabbitMqClientService.BrokerOptions.ExchangeName,
routingKey: routingKey,
arguments: null);

channel.BasicQos(0, prefetchCount, false);

return channel;
}

Expand All @@ -28,4 +31,6 @@ public AsyncEventingBasicConsumer GetConsumer(IModel channel)

return consumer;
}

public void Dispose() => _connection?.Dispose();
}

0 comments on commit f243d6d

Please sign in to comment.