Search code examples
c#.netrabbitmqeasynetq

EasyNetQ - receiving from existing queue


I am looking at using EasyNetQ for interacting with RabbitMQ and wondering if it can support following case:

  1. Queue is declared externally with some arbitrary arguments (e.g. x-message-ttl)
  2. Client code using EasyNetQ sends and receives messages from that queue.

Possibilities I have found are:

  • Simple IBus API requires that queue has default parameters
  • Advanced IAdvancedBus API allows to specify arguments of the declared-queue but not all (e.g. x-max-length can't be set)

The question is can I just use existing queue with custom parameters and without need to specify them?


Solution

  • If the queue already exists and you know its name, couldn't you use the IAdvancedBus.Consume<T> method (and not worry about IAdvancedBus.QueueDeclare)?

    For example:

    var queueName = "TheNameOfYourExistingQueue";
    var existingQueue = new EasyNetQ.Topology.Queue(queueName, false);
    
    // bus should be an instance of IAdvancedBus
    bus.Consume<TypeOfYourMessage>(existingQueue, 
       (msg, info) => 
          {
             // Implement your handling logic here
          });
    

    Note that EasyNetQ might have trouble automatically deserializing messages into instances of TypeOfYourMessage. If that is the case, one way to solve it would be to bypass EasyNetQ's message serializer so that you can access the byte array of the message directly. Use the following overload to Consume if you wish to go that route:

    void Consume(IQueue queue, Func<Byte[], MessageProperties, MessageReceivedInfo, Task> onMessage);