EasyNetQ - receiving from existing queue
Asked Answered
S

2

6

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?

Stucco answered 19/5, 2015 at 15:36 Comment(0)
A
9

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);
Amends answered 10/6, 2015 at 13:22 Comment(0)
J
1

Even with solution 10477404, parameters like isDurable, isExclusive, isAutoDelete, and arguments must match the original Queue declaration to avoid creating a new one.

For safety, and if you have a way to know the original queue declaration parameters, use them to create the queue with IAdvancedBus.QueueDeclare() or IAdvancedBus.QueueDeclareAsync()

Jennyjeno answered 30/11, 2022 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.