Subscribe to a queue in RabbitMQ

The Devprime platform’s ‘AddSubscribeRabbitMQ’ method is used to subscribe to a queue on an exchange in RabbitMQ. This method creates a subscription that allows the queue to receive messages from the exchange, based on the type of exchange and the routing key provided.

Parameters

  • alias (string, optional): Optional parameter with the name of the Stream Alias as defined in the Stream Adapter configuration. The default value is “Stream1”.

  • exchangeName (string): Name of the exchange from which the queue will receive messages.

  • exchangeType (ExchangeType): Type of the exchange. It can be:

    • Direct: Routes messages to queues with an exact routing key.
    • Fanout: Routes messages to all queues connected to the exchange.
    • Topic: Routes messages to queues based on routing key patterns.
    • Headers: Routes messages based on message headers.
  • queueName (string): Name of the queue that will be signed to receive messages from the exchange.

Return

  • List<(bool Ready, string Queue)>: Returns a list of tuples where each tuple contains:
    • Ready (bool): Indicates whether the signature was successfully created.
    • Queue (string): Name of the queue that was signed.

Here is an example of how to use the AddSubscribeRabbitMQ method in your application:

Adding the subscription to a Queue

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var exchangeName = "myExchangeName";
var queueName = "myQueueName";
var success = Dp.Stream.AddSubscribeRabbitMQ(exchangeName, ExchangeType.Direct, queueName);
  if (success)
  {
      Console.WriteLine("Assinatura criada com sucesso.");
  }
  else
  {
      Console.WriteLine("Falha ao criar a assinatura.");
  }

Considerations

  • Make sure that exchangeName and queueName are correctly configured and exist in RabbitMQ.
  • The exchange type (exchangeType) must match the routing strategy you want to implement.
  • Native implementations of RabbitMQ are specific to this platform and cater to particular use cases. If you migrate to another streaming platform, you’ll need to adjust your code to use Devprime’s standard methods or the new platform’s specific methods.
Last modified November 20, 2024 (61099f59)