Contact Free trial Login

Tutorial: Create a Queue and Message Exchange

This tutorial helps you send and receive messages using Anypoint MQ using a publish/subscribe queue mechanism. Anypoint MQ provides a user interface built into Anypoint Platform that lets you send messages to a queue or message exchange, and then read the messages from a queue. Anypoint MQ also provides a connector that works with Anypoint Studio. See Anypoint MQ Connectors for information about Mule 3 and Mule 4 connectors that you can use with Anypoint Studio.

Prerequisites

  1. Read Anypoint MQ Terminology.

  2. Create an Anypoint Platform account to configure an environment, user permissions, register applications in MQ, and to create queues and message exchanges. You can access Anypoint MQ in the Anypoint Platform and the left navigation bar.

  3. An Enterprise license is required to use MQ. Contact the MuleSoft Sales team to get a free trial.

  4. Download and install Anypoint Studio.

  5. You need a curl command to test Anypoint MQ in this tutorial.

Queue and message exchange names can contain alphanumeric characters (a-z, A-Z, 0-9), dots (.), and dashes (-). No other characters are allowed. Queue and message exchange names are limited to 127 characters in length.

Create a Queue

A queue provides a temporary area for holding messages. Applications push messages to the queue, and other applications consume the messages. In Anypoint MQ, you can read messages, return the messages for later viewing, or delete a message. By default, messages return to the waiting area so that other applications can read them. Messages persist until the time to live (TTL) value expires. By default, a message persists in Anypoint MQ for up to 7 days. The minimum message TTL is 1 minute and the maximum is 14 days.

  1. Log into Anypoint Platform and click MQ.

  2. Click Destinations:

    mq destinations option
  3. Click the blue plus icon mq blue plus icon

  4. In the drop-down, click Queue:

    mq create choices
    mq create demo queue
  5. In the Create Queue screen, specify a name for the queue. This tutorial uses MyDemoQueue.

    The TTL is the time to live, that is, how long a message can stay in a queue before being deleted. The default lock TTL determines how long a message is unavailable to other applications. A locked message does not block other messages from being read.

  6. When done, click Save Changes:

    The queue creates and you are returned to the Destinations screen.

    Where you click on a queue entry in the Destinations screen governs what you see next:

    mq entryui

If you need to delete a queue, see How do I delete a queue? in the Anypoint MQ FAQ.

Send a Message to a Queue

  1. Log into Anypoint Platform and click MQ.

  2. Click Destinations.

  3. Click the MyDemoQueue entry in Destinations to view details about the queue.

  4. Click the left side of the queue entry to open the Messaging feature, or click the right side of the queue entry and click the arrow to the right of the queue name:

    mq access messaging2
  5. In the settings page, click Message Sender:

    mq click msg sender2
  6. Type text in the Payload such as Hello Mules (leave the Type field set to Text):

    mq msg sender text payload2
  7. Click Send.

Verify the Message in a Queue

  1. Click Destinations.

  2. Click the right side of the queue entry to view details and see the number of messages in the queue:

    mq num queued msgs

Get a Message From a Queue

  1. In Anypoint Platform > MQ, click Message Browser:

    mq click msg browser2
  2. Click Get Messages.

    mq get messages2
  3. Click the message ID value to view the message.

    mq click id2
  4. If you want to return the message to the queue, so other applications can read the message, click the Return Messages icon - this is the default behavior. If you switch screens back to the Message Sender or to Destinations, messages automatically return to the queue. In Anypoint MQ, returning the messages to the queue is known as nack - the message is not altered. However, the time to live (TTL) value you set when you created your queue determines how long the message is available before Anypoint MQ deletes it.

    mq click retmsgs2

    Alternatively, you can delete the message by clicking the trash can icon. Anypoint MQ provides three modes for how messages delete after they are viewed. For more information, see Acknowledge a Message.

    mq message delete trash can icon2

Now you are able to send and receive messages in Anypoint MQ. In the next section, you can try alternate ways of formatting messages.

Send a CSV or JSON Message

  1. Click Message Sender.

  2. Set the Type to JSON.

  3. Set the Payload to:

    {
    "animal that walks":"dog",
    "animal that swims":"fish",
    "animal that flies":"parrot"
    }
  4. Click Message Browser and the message ID to view the message:

    mq json get msg2

To send a CSV message:

  1. Click Message Sender.

  2. Set the Type to CSV.

  3. Set the Payload to:

    "dog",
    "fish",
    "parrot"
  4. Click Message Browser and the message ID to view the message.

Register a Client Mule Application

  1. Log into Anypoint Platform and click MQ.

  2. Click Client App:

    mq click client apps
  3. Click the blue plus icon mq blue plus icon.

  4. Specify an application name such as DemoClientApp and click Save Changes.

    mq create client app name
  5. Leave the Client Apps window open as you need to copy and paste the client ID and client secret into the configuration for Anypoint Studio.

Configure Anypoint MQ in Design Center

Disable password programs such as LastPass or Okta in your browser before using Design Center. Otherwise, the password program inserts passwords in the HTTP Listener’s TLS tab. The passwords in the TLS tab cause the app to fail.

Before starting to configure an application in Design Center, leave a browser window open to Anypoint Platform > MQ. Determine the queue you want to access and have a Mule application available so you can copy its client ID and secret.

  1. In Anypoint Platform > Design Center, click Create > Mule Application:

    mq dc create
  2. Provide a project name and click Create. In this tutorial, the app is called mqdemo:

    mq dc new mule app
  3. In the help wizard, click Go straight to Canvas.

    mq dc go straight
  4. Select a trigger for the action that starts Anypoint MQ connector access. The simplest trigger is HTTP Listener, which we use in this tutorial to initiate access using a curl command. Alternatively you can use Anypoint MQ as a subscriber to retrieve messages from a destination name, or specify a scheduler to accept input at prescribed times.

    Type in a few letters in the trigger search box, such as "HT" and select the HTTP Listener:

    mq dc http find
  5. In the HTTP Listener, set the Path to the /mq/{messageId} value.

    mq dc http path
  6. Click the plus symbol to the right of the trigger:

    mq dc click plus
  7. In the Component menu, select the Mule Anypoint MQ Connector.

  8. For this tutorial, select the Publish operation:

    mq dc publish op

    For the other operations, if you were constructing your Mule application to receive a message sent from a queue, you would use the Consume operation. If your application had tests to determine if message processing succeeded, you would use the Ack operation to acknowledge that a message was received and could be deleted. Similarly if message processing failed, you would use the Nack operation to negatively acknowledge the message. In the case of Nack, the message would be returned to the queue to await accessing.

  9. In the Destination, specify the name of queue you want to access from your Anypoint Platform > MQ browser window, such as MyDemoQueue that was created earlier in this tutorial. Then click Set Up.

    mq dc destination
  10. Copy the client ID and client secret from your Anypoint Platform > MQ browser window. In the MQ window, click Client Apps and click the name of an application. Click Copy for each ID and secret values and paste each in the Design Center window.

    mq dc client id secret
  11. Click Test to ensure that you have access to your Mule application. If successful, the green Test Successful box appears. If not, ensure that the client ID, client secret, and URL values are correct. If the URL does not match that in Anypoint Platform > MQ, copy the URL from Anypoint MQ to the Design Center screen and click Test again.

    mq dc test connection
  12. Click the Run button.

    mq dc run btn
    mq dc running

Test Your Design Center App

  1. Run this command from a command prompt to send the body value as an Anypoint MQ message. The 100 value in the URL specifies the message ID value per the /mq{messageId} value in the HTTP Listener, which is the identifier for your message:

    curl -X POST \
      http://DEPLOYMENT_URL/mq/100 \
      -H 'content-type: application/json' \
      -d '[  {
        "body": "Welcome to MuleSoft!"
      }
    ]'

    You can find the value for DEPLOYMENT_URL in the detail for your application in the Design Center window:

    mq dc details
  2. In Anypoint Platform > MQ, click the name of the queue you specified as the Destination in Design Center. Click Message Browser:

    mq msg browser
  3. Click Get Messages:

    mq get messages
  4. Click the ID value in the message page to view the message contents:

    mp dc test in mq
  5. Try changing the body text in the curl command and repeating the steps to view the message.

You can now configure Anypoint MQ in Design Center, submit REST API commands to the MQ Broker API, and use Anypoint MQ to view the results.

Create a Studio 7 Mule Application

You can use Anypoint Studio 7 to create a Mule application that uses Anypoint MQ much the same way as you created in Design Center. Studio 7 enables you to create the Mule application, you can test the program, and then package the Mule application into a zip file to put into Anypoint Platform > Runtime Manager. Runtime Manager serves the Mule application so you can submit curl commands as you did in the previous Configure Anypoint MQ in Design Center section. See Anypoint MQ Connectors for more information.

Prerequisites:

  • Studio 7 requires that you install Oracle Java SDK 8. Earlier versions of Java or versions after version 8 are not supported.

  • In Anypoint Platform, change the environment to Production and ensure that any values you set and get in Anypoint Platform > MQ are also in the Production environment.

  • If you have not done so already, download and install Anypoint Studio 7.

Configure Studio 7 for Anypoint MQ

  1. Start Studio, click File > New > Mule Project, and give the the project a name such as mq.

  2. Click the Exchange icon in the toolbar:

    mq st7 exchange
  3. In Exchange, click Login and supply your Anypoint Platform username and password.

  4. Search for the Anypoint MQ Connector, click Add to project, and click Proceed.

  5. Click HTTP in the Mule Palette:

    mq st7 mule palette
  6. Click Listener and drag it to the Studio canvas.

  7. Click Anypoint MQ and drag Publish to the right of the Listener on the canvas:

    mq st7 flow
  8. Click the Listener icon. Set General > Path to /mq/{messageId}:

    mq st7 listener path
  9. Click the green plus button to the right of the Connector configuration field.

  10. Set Host to 0.0.0.0 and Port to 8081. Leave all other values as they are:

    mq st7 globals
  11. Click the MQ Publish icon. Set Destination to the name of a queue in Anypoint Platform > MQ. Ensure that the queue is in the Production environment.

  12. Click the green plus button to the right of the Connector configuration field.

  13. Set the URL field to the URL in MQ:

    mq url
  14. In MQ, click Client Apps. If needed, create a new app. Copy the client ID and client secret to the clipboard and paste each into Studio. Ensure that MQ displays the Production environment.

    mq st7 id secret
  15. Click Test Connection to ensure the values are correct.

  16. Save your project. Right click the name of your app in Package Explorer and click Anypoint Platform > Deploy to CloudHub.

    mq st7 deploy

    If you see this message: You do not have permission to deploy new applications in this environment. Please select an existing application to redeploy, change the environment so that it is not the Design environment. For example, set the environment to Production or Sandbox. To change the environment, click Design in the upper left corner, and select another environment.

    You may also need to change the name of the application to deploy it successfully.

  17. Browse to Anypoint Platform > Runtime Manager. Ensure the application is running. Click the application and copy the App url value:

    mq app url
  18. In the following command, replace DEPLOYMENT_URL with the App url value from Runtime Manager. Run this command from a command prompt to send the "body" value as an Anypoint MQ message.

    curl -X POST \
      http://DEPLOYMENT_URL/mq/100 \
      -H 'content-type: application/json' \
      -d '[  {
        "body": "Message via Runtime Manager"
      }
    ]'

    If you see a 404 message for destination not found, verify that the name of the queue in the Studio 7 MQ Publish > Destination field matches the name of the queue in Anypoint Platform > MQ.

    You can redeploy your Mule application by right-clicking the project in Studio 7 and selecting Anypoint Platform > Deploy to CloudHub. This command lets you update the app in Runtime Manager.

  19. In Anypoint Platform > MQ (in the Production environment), click the queue name, click Message Browser, Get Messages, and click the message to view the content of the message:

    mq runtime mgr result

Studio 7 Summary View

The following view shows the Mule application in Package Explorer, the position of the HTTP Listener and MQ Publish, and the Publish properties:

mq st7 big picture

Studio 7 XML

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    xmlns:anypoint-mq="http://www.mulesoft.org/schema/mule/anypoint-mq"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
    http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/anypoint-mq
http://www.mulesoft.org/schema/mule/anypoint-mq/current/mule-anypoint-mq.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <anypoint-mq:default-subscriber-config name="Anypoint_MQ_Default_subscriber"
        doc:name="Anypoint MQ Default subscriber" >
        <anypoint-mq:connection clientId="CLIENT_ID" clientSecret="CLIENT_SECRET" />
    </anypoint-mq:default-subscriber-config>
    <flow name="mqFlow">
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config"
            path="/mq/{messageId}">
            <ee:repeatable-file-store-stream />
        </http:listener>
        <anypoint-mq:publish doc:name="Publish"
            config-ref="Anypoint_MQ_Default_subscriber"
            destination="MqDemoQueue">
            <ee:repeatable-file-store-stream />
        </anypoint-mq:publish>
    </flow>
</mule>

Create a Studio 6 Mule Application

This section applies only to Anypoint Studio 6. For Studio 7, see the earlier section Create a Studio 7 Mule Application.

  1. In Studio 6, click the Exchange icon in the Studio taskbar.

  2. Click Login in Anypoint Exchange.

  3. Search for the Anypoint MQ connector and click Install.

  4. Follow the prompts to install the connector.

  5. Create a new Mule Project. Click File > New > Mule Project. Name the project mqdemo.

  6. Search for http and drag the HTTP connector to the Studio canvas.

    mq connector visual flow
  7. Search for set and drag Set Payload to the canvas.

  8. Search for mq and drag the Anypoint MQ connector icon to the canvas.

  9. Search for logger and drag Logger to the canvas.

  10. Click the HTTP connector and click the green plus sign to the right of Connector Configuration.

  11. In the HTTP Connector’s Global Element Properties menu, accept the defaults, and click OK.

  12. Set the Path to /mq/{messageId}:

    mq http path
  13. Click Set Payload in the canvas and set the value to:

    #[message.inboundProperties.'http.uri.params'.messageId]
    mq set payload
  14. Click the Anypoint MQ connector, and click the green plus sign to the right of Connector Configuration.

  15. In Anypoint MQ Connector’s Global Element Properties window, add the information from Anypoint Platform:

    mq connector properties
  16. Copy the Anypoint Platform’s Client App > Client App ID value to the Studio Client ID field.

    mq client id and secret values
  17. Copy the Anypoint Platform’s Client App > Client Secret value to the Studio Client Secret field.

  18. Click OK.

  19. Click the Operation field and specify the publish operation.

    mq operation publish
  20. Specify the Destination as MyDemoQueue that you set in Anypoint Platform:

    mq destination queue2
  21. Click the Logger and set the Message field to MQ Message: #[payload]:

    mq logger properties
  22. Save your project.

You can test your application using the same curl command as in Create a Studio 7 Mule Application - deploy your Mule application from Studio to CloudHub and after the application starts in Runtime Manager, you can send a curl command to send a message to MQ.

Studio 6 XML

After you configure your application using the Anypoint MQ Connector, you can check your configuration against this XML.

Click Configuration XML in Studio and compare your XML to the following. If needed, you can make corrections.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:anypoint-mq="http://www.mulesoft.org/schema/mule/anypoint-mq"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/anypoint-mq
http://www.mulesoft.org/schema/mule/anypoint-mq/current/mule-anypoint-mq.xsd">
    <http:listener-config name="HTTP_Listener_Configuration"
    host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <anypoint-mq:config name="Anypoint_MQ_Configuration"
    doc:name="Anypoint MQ Configuration">
        <anypoint-mq:provider
        url="https://mq-us-east-1.anypoint.mulesoft.com/api/v1"
        clientId="<client_ID_value>" clientSecret="<client_secret-value>"/>
    </anypoint-mq:config>
    <flow name="mqdemoFlow">
        <http:listener config-ref="HTTP_Listener_Configuration"
        path="/mq/{messageId}" doc:name="HTTP"/>
        <set-payload value="#[message.inboundProperties.'http.uri.params'.messageId]"
        doc:name="Set Payload"/>
        <anypoint-mq:publish config-ref="Anypoint_MQ_Configuration"
        doc:name="Anypoint MQ" destination="MyDemoQueue"/>
        <logger message="MQ Message = #[payload]" level="INFO"
        doc:name="Logger"/>
    </flow>
</mule>

Create a Message Exchange

A message exchange groups one or more queues so that a message sent to the message exchange goes to all bound queues.

  1. Log into Anypoint Platform > MQ.

  2. Click Destinations.

  3. Click the blue plus icon mq blue plus icon

  4. Click Exchange.

  5. Specify the name MyDemoExchange.

  6. Click the checkbox to bind MyDemoQueue to this message exchange.

  7. Click Save Changes.

  8. In the Destinations screen, click the message exchange Type to list its details.

To delete a message exchange, see How do I delete a Message Exchange? in the Anypoint MQ FAQ.

Bind a Queue to a Message Exchange

  1. Log into Anypoint Platform and click MQ.

  2. Click Destinations.

  3. Click the left side of the message exchange entry in Destinations.

    Where you click on a message exchange entry, the Destinations table governs what you see next:

    mq where to click x2

  4. In the message exchange menu, click Bind for each queue you want to bind to the message exchange:

    mq bind queue to exchange2

Send a Message to a Message Exchange

Sending a message to a message exchange is very similar to sending a message to a queue. The only difference is that you can get the message from any queue bound to a message exchange.

  1. Log into Anypoint Platform > MQ.

  2. Click Destinations.

  3. Click the MessageExchange entry in Destinations to view details about the message exchange.

  4. Click the MessageExchange link in the details screen to access the Message Sender:

    mq exchange msg access2
  5. Click Message Sender:

    mq exchange msg sender2
  6. Type the contents of the Payload and click Send:

    mq exchange payload2

You can now use the Message Browser to get the message from MyDemoQueue as described in Get a Message From a Queue.

You can also send comma-separated value (CSV) or JSON content in the payload by changing the message Type value. For more information, see Send a CSV or JSON Message.

See Also

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub