Add an exchange on RabbitMQ

The Devprime platform’s ‘AddExchangeRabbitMQ’ method is used to create a new exchange on RabbitMQ. This method supports a variety of exchange types, including Direct, Fanout, Topic, and Headers, allowing for flexible configuration of message routing. In addition, it allows for custom configuration of exchange properties through the ‘CustomExchangeProperties’ class.

Parameters

  • alias (string): 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. This is the exchange’s unique identifier on RabbitMQ.

  • exchangeType (ExchangeType): Type of the exchange, which can be one of the following:

    • Direct: Routes messages to queues that have 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.
  • customProperties (CustomExchangeProperties, optional): Additional properties for configuring the exchange. The class CustomExchangeProperties allows you to specify:

    • Durable (bool): Defines whether the exchange should be durable. The default is true.
    • AutoDelete (bool): Defines whether the exchange should be automatically deleted when there are no queues connected. The default is false.
    • Arguments (IDictionary<string, object>): Additional arguments for exchange-specific configuration. It can be null if there are no additional arguments.

Return

  • bool: Returns true if the exchange is successfully created; otherwise, returns false.

Here are examples of how to use the AddExchangeRabbitMQ method with and without custom properties:

**1. Creating a Direct exchange without custom properties:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Criação de uma exchange do tipo Direct
var exchangeName = "myExchangeName";
var success = Dp.Stream.AddExchangeRabbitMQ(exchangeName,ExchangeType.Direct,null);
if (success)
{
    Console.WriteLine("Exchange criada com sucesso.");
}
else
{
    Console.WriteLine("Falha ao criar a exchange.");
}

**2. Creating a Topic exchange with custom properties:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var exchangeName = "myExchangeName";
var properties = new CustomExchangeProperties
    {
        Durable = true, // Explicitly durable
        AutoDelete = false, // Do not auto-delete
        Arguments = new Dictionary<string, object>
        {
            { "x-message-ttl", 60000 }, // Message time-to-live in milliseconds
            { "x-max-length", 1000 }    // Maximum number of messages in the queue
        }
    };
var success = Dp.Stream.AddExchangeRabbitMQ(exchangeName, ExchangeType.Direct, properties);

Considerations

  • Make sure that the exchangeName is unique within RabbitMQ to avoid conflicts.
  • Choose the appropriate exchangeType based on your message routing strategy.
  • Use CustomExchangeProperties to configure advanced behaviors such as durability, auto-delete, and specific arguments.
  • 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)