With the new Mule 4 approach, the AMQP 'publish' operation relies only on its input parameters to completely build the AMQP Message to be published.
For example, if we wanted to send a high priority AMQP Message with only a part of the payload in the body, and associate that Message to a group, we’ll need to:
<1>) Use transform to set the payload to what the Message body is expected to be.
<2>) Convert the resulting stream into a String to send it as a text message.
<3>) Set an AMQP property with priority as key to set the AMQP Message Priority.
Mule 3 Example: Sending a prioritized Message as part of a Group
<flow name="AmqpTransportOutbound">
<http:listener config-ref="HTTP_Listener_Configuration" path="/orders"/>
<dw:transform-message> (1)
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
order_id: payload.id,
supplier: payload.warehouse
}]]></dw:set-payload>
</dw:transform-message>
<object-to-string-transformer/> (2)
<amqp:outbound-endpoint exchangeName="testExchange" connector-ref="AMQP_Connector" >
<message-properties-transformer scope="outbound">
<add-message-property key="priority" value="9"/> (3)
</message-properties-transformer>
</amqp:outbound-endpoint>
</flow>
The same results can be achieved in Mule 4 using the AMQP Connector with the following configuration:
Mule 4 Example: Sending a prioritized Message as part of a Group
<flow name="AMQPConnectorPublish">
<http:listener config-ref="HTTP_Listener_config" path="/orders"/>
(2)
<amqp:publish config-ref="AMQP_Config" exchangeName="targetExchange">
<amqp:message> (1)
<amqp:body>#[output application/json ---
{
order_id: payload.id,
supplier: payload.warehouse
}]</amqp:body>
<amqp:properties priority="3"/> (3)
</amqp:message>
</amqp:publish>
</flow>
1) There’s no need of the transform component, since the body of the Message is created inline, thus the payload remains unmodified.
2) The object-to-string transformer was also removed, since the Connector can automatically handle the transformation output.
3) Priority is set as a property in the AmqpMessage operation and does not rely on the user knowing the exact key.
As a summary, when publishing a Message in 3.x with the AMQP transport, we relied on the AmqpMessage payload, and outbound properties to configure the creation of the AMQP Message, which meant a deeper knowledge of how the transport worked. In 4.x, the AMQP Connector exposes every configurable element as a parameter in the scope were it belongs, thus exposing all the AMQP functionality in a clearer way.