Contact Free trial Login

Anypoint MQ Publish Operation - Mule 4

The Publish operation enables you to create a new Anypoint MQ message and send it to the specified destination: queue, FIFO queue, or message exchange. With it, you can configure both the content of the message and all the headers and properties you need.

Outgoing Message Structure

Messages contain these parameters:

  • body

    Contains the body of the published Anypoint MQ message.

  • properties

    Represents a simple key-value map of strings that are the message properties of the message.

Send a Message

When used in its default form, the connector publishes the content found in the incoming message payload as body of the Anypoint MQ message, serialized as a string, to the declared destination:

Anypoint MQ Publish Operation
<anypoint-mq:publish destination="invoiceQueue" config-ref="Anypoint_MQ_config"/>

This operation produces the following output:

  • A copy of the body sent as payload

    For example, if you are sending a "Hello world" message, the resulting payload is the "Hello world" string.

  • The message messageId value in the message attributes

Anypoint MQ Publish Operation Output Attributes

Configure User Properties

You can define properties for outgoing messages for example, to provide compatibility with other messaging systems or to communicate the content type of a message.

Use the properties parameter of the Publish operation to configure these properties directly in the message. Define the properties parameter as a key-value map of strings using DataWeave:

Anypoint MQ Publish With Properties
<anypoint-mq:publish config-ref="Anypoint_MQ_Config"
  <anypoint-mq:properties><![CDATA[#[output application/java ---
          AUTH_TYPE: 'jwt',
          AUTH_TOKEN: attributes.queryParams.token

Transform the Message Body

If the payload is not in the correct format, you can transform it to the correct format.

To transform a payload, declare an inline DataWeave transformation in the body parameter of the Publish operation:

Anypoint MQ Publish With Transformation
<anypoint-mq:publish config-ref="Anypoint_MQ_Config"
      <anypoint-mq:body ><![CDATA[#[output application/json ---
        items: payload.invoice.items

In this case, the result of the operation is the transformed value as JSON.

Advanced Configurations

You might want to set a unique custom message ID instead letting Anypoint MQ generate it. You can also add the content type of the message body as a property.

Set a Custom Message ID

Instead of letting the Anypoint MQ service generate a unique messageId value for the new published message, you can set a custom messageId value for your outgoing message using the Message id field in the Advanced tab of the main screen:

Anypoint MQ Publish Advanced Tab

The ID of an Anypoint MQ message must be unique. Make sure to choose a unique custom ID to avoid unwanted side effects of duplicated IDs. In FIFO queues, messages with duplicate IDs are overwritten.

Propagate Content Type

To specify whether the content type of the message body is propagated as a property of the Anypoint MQ message or not, use the sendContentType parameter.

Setting this parameter to true automatically adds a contentType property to the outgoing message. This can be useful to declare that a message body is in application/json or application/xml format.

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.