Search code examples
c#rabbitmqeasynetq

EasyNetQ Topic Routing


Hi I have a question I have a main service and two client service, client service can be identified by unique id, not to communicate between sewer and clients I use RabbitMq with EasyNetQ, so what I want is to use single queue with some sort of routing so client will know, that this is not message is not for me and do not consume it. I thought topic based routing will do but something is wrong, and all clients get message alternately, once one client one another. And i do not want to use many queues and number of those clients can grow.

So my subscriber (client service side) looks like:

var topic = $"ProjectId.{ProjectId}.CabinId.{CabinId}";
var responseHandler = new Func<ManualServiceRequestMessage, Task>
(response => Task.Factory.StartNew(() =>
{
    Console.WriteLine($"!!!!!!!!! {topic} !!!!!!!!!!");
    Console.WriteLine($"Response pid {response.ProjectId} cid {response.CabinId}");
})
.ContinueWith(task =>
{
   if (!task.IsCompleted && task.IsFaulted)
   {
       Console.WriteLine($"Message receive problem {response.ProjectId} frame type {response.CabinId} response id {response.RequestHash}");
   }
}));

bus.SubscribeAsync<ManualServiceRequestMessage>("LiveServer.ManualRequestQueue", responseHandler, x => x.WithTopic(topic));

Publisher (Server side)

var topic = $"ProjectId.{projectId}.CabinId.{cabinId}";
bus.PublishAsync(requestService, topic);

So anything I can do to do this?


Solution

  • You can solve this on the subscriber (client) side: Instead of using bus.SubscribeAsync, call bus.Advanced.Bind and then bus.Advanced.Consume. The Advanced field is of type IAdvancedBus and provides access to more convenient functions for queue communication and configuration. The IAdvancedBus.Bind method accepts a routing key and sets up basic configuration your subscriber needs to communicate with RabbitMQ, and the IAdvancedBus.Consume method actually starts listening for incoming messages asynchronously.