Publishing a message to an exchange or queue in RabbitMQ
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 to which the message will be published. -
exchangeType(ExchangeType?, optional): 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.
If not specified, the default is used as per the exchange configuration.
-
routingKey(string): Routing key used to direct messages from the exchange to the associated queues. -
eventMessage(BasicEvent): Event message that will be published on the exchange. -
customProperties(CustomMessageProperties, optional): Custom properties for the message, such as Time-to-Live (TTL), persistence, and other specific characteristics.
Return
void: This method does not return a value. It performs the publication of the message to the specified exchange.
Here are examples of how to use the PublishRabbitMQ method in your application:
1. Direct Publish to Queue Without Using Exchange
In this scenario, it is not necessary to inform an exchange
|
|
2. Publishing using Exchange Direct without CustomMessageProperties
To configure this scenario, you need to associate the Exchange “myExchangeName” of type Direct with the “myQueueName” queue.
|
|
3. Publishing using Exchange Direct using TTL
To configure this scenario, you need to associate the Exchange “myExchangeName” of type Direct with the “myQueueName” queue. We used CustomMessageProperties to set the persistence parameters and expiration time (TTL). The published event will have a set time to expire.
|
|
4. Publishing using Exchange Fanout with two queues as the target
To configure this scenario, you need to associate the Exchange “myExchangeName” of type Fanout with the queues “myQueueName1” and “myQueueName2”. In this case, you do not need to specify the queue name in Publish. Exchange will automatically distribute the event to both queues.
|
|
5. Publishing using Exchange Topic with two queues as the target
To configure this scenario, you must associate the Exchange “myExchangeName” of type Topic with two queues:
- Bind to the queue “myQueueName1” with the Routing Key:
"quick.ecommerce.#" - Bind to the queue “myQueueName2” with the Routing Key:
"#.logs"
In this example, we will make three posts, ranging from routingKey. The exchange will distribute the messages in such a way that one of them will be delivered to both queues, while the others will be delivered to only one of the queues.
|
|
6. Publishing using Exchange Headers with two queues as the target
To configure this scenario, you need to associate the Exchange “myExchangeName” of type Headers with two queues. The first bind will be to the queue “myQueueName1” using the following key/value arguments:
- Arguments 1:
{ "format", "pdf" } - Arguments 2:
{ "type", "report" } - Arguments 3:
{ "x-match", "all" }
The second bind will also be for the queue “myQueueName2”, with the following arguments:
- Arguments 1:
{ "format", "zip" } - Arguments 2:
{ "x-match", "any" }
It is important to note that Exchange type Headers does not use the Routing Key setting during bind.
In this example, we will perform three publications, varying the parameters of Arguments. The exchange will be responsible for distributing the messages, and at the end, one of the events will be directed to “myQueueName1” and the other two to “myQueueName2”.
|
|
Considerations
- Make sure that
exchangeNameis correctly configured and exists in RabbitMQ before attempting to publish the message. - Adjust
exchangeTypeandcustomPropertiesas needed for your message publishing and routing strategy. - 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.