0

Given two subscribers, processing synchronously Message objects from RabbitMQ queue, one at a time for 3s,

when publisher send 4 messages:

Message { Text: 1 }

Message { Text: 2 }

Message { Text: 3 }

Message { Text: 4 }

Is it possible to divide work this way:

Subscriber1 - 1 - 00:00:00

Subscriber2 - 2 - 00:00:00

Subscriber1 - 3 - 00:00:03

Subscriber2 - 4 - 00:00:03

so that subscribers start immediately with first two messages and then, when they finish, with next ones? Order is not important for me. I would like to use EasyNetQ, because of its nice api, but default .NET/C# RabbitMQ client would also be fine.

This code does not work as expected:

var Bus = RabbitHutch.CreateBus("host=localhost");

Bus.Subscribe<Message>("subscriptionId", x =>
{
    Console.WriteLine("Subscriber1 - " + x.Text + " - " + DateTime.Now.ToString("HH:mm:ss"));
    Thread.Sleep(3000);
});

Bus.Subscribe<Message>("subscriptionId", x =>
{
    Console.WriteLine("Subscriber2 - " + x.Text + " - " + DateTime.Now.ToString("HH:mm:ss"));
    Thread.Sleep(3000);
});

Bus.Publish(new Message("1"));
Bus.Publish(new Message("2"));
Bus.Publish(new Message("3"));
Bus.Publish(new Message("4"));

Anyone?

Maleowy
  • 37
  • 7

1 Answers1

1

I figured it out. Should be like this:

Bus = RabbitHutch.CreateBus("host=localhost;prefetchcount=1");

Bus.SubscribeAsync<Message>("subscriptionId", async x =>
{
    Console.WriteLine("Subscriber1 - " + x.Text + " - " + DateTime.Now.ToString("HH:mm:ss"));
    await Task.Delay(3000);
});

Bus.SubscribeAsync<Message>("subscriptionId", async x =>
{
    Console.WriteLine("Subscriber2 - " + x.Text + " - " + DateTime.Now.ToString("HH:mm:ss"));
    await Task.Delay(3000);
});

Subscribers need to be async, but key part is also to set prefetchcount to 1.

Maleowy
  • 37
  • 7
  • Setting up `prefetchcount` to 1 might be not a good idea if you care about performance of consumer. Assuming that you want to handle your messages in parallel you care about performance. So first of all maybe it is better to have one `SubscribeAsync` but leave `prefetchcount` with default value or find your own value. – Pavel F Sep 22 '19 at 18:25