Skip to content

Simple adapter that provides ability to work with RabbitMQ consumer logic in AsyncEnumerable way

Notifications You must be signed in to change notification settings

ntulenev/RabbitMQAsyncEnumerableAdapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

RabbitAdapter

Adapter that helps use RabbitMq consumer as AsyncEnumerator

var factory = new ConnectionFactory() { HostName = "localhost" };

using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();

channel.QueueDeclare(queue: "hello",
                     durable: false,
                     exclusive: false,
                     autoDelete: false,
                     arguments: null);

var consumer = new EventingBasicConsumer(channel);

// Create rabbit adapter with internal buffer (of 10 items)
RabbitAdapter ra = new RabbitAdapter(10, messageId => channel.BasicAck(messageId, false));

// Connect adapter with consumer event handler
consumer.Received += ra.ConsumeData;

channel.BasicConsume(queue: "hello",
                     autoAck: false,
                     consumer: consumer);

// Use adapter as async enumerator
await foreach (var data in ra.WithCancellation(CancellationToken.None))
{
    var body = data.Body.ToArray();
    var message = Encoding.UTF8.GetString(body);
    Console.WriteLine(message);
}

About

Simple adapter that provides ability to work with RabbitMQ consumer logic in AsyncEnumerable way

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages