Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Service Bus multiple listeners all receive the same message ( BrokeredMessage)

I used to use RabbitMQ as a messaging platform and I never had any issues with it - unfortunately, we've recently moved our infrastructure over to Azure and they don't provide RabbitMQ servers so I thought about trying out the Service Bus extension.

I have one writer and multiple readers. Currently, the readers will each read a different message (message competing pattern - e.g. good for load balancing).

What I want, is that all the readers get the same message and process it themselves.

The reader(s):

string connectionKey = "....";
        this.client = QueueClient.CreateFromConnectionString(connectionKey, "dev");
        this.client.OnMessage((message) =>
        {
            try
            {
                Console.WriteLine("Message received: " + message.GetBody<string>());
                Console.WriteLine("Message ID: " + message.MessageId);
              //  message.Complete();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception " + e.Message);
            }
        });

The writer:

Console.WriteLine("Sending message " + message);
        BrokeredMessage msg = new BrokeredMessage(message);
        this.client.Send(msg);

I've been looking for a solution for two hours and couldn't find anything. In RabbitMQ, that would be the default behavior.

Image showing pattern of what I have vs. what I need

like image 271
TheWalkingPanda Avatar asked May 21 '16 19:05

TheWalkingPanda


1 Answers

Azure Service Bus supports multiple paradigms, one of which is called "Topics":

How to use Service Bus topics and subscriptions

In contrast with Service Bus queues, in which each message is processed by a single consumer, topics and subscriptions provide a "one-to-many" form of communication, using a publish/subscribe pattern.

Link above demonstrates core topics concepts as well as code samples.

like image 164
Admir Tuzović Avatar answered Sep 30 '22 02:09

Admir Tuzović