Contact Us 1-800-596-4880

Tutorial: Create a Queue and Send Messages to It

Use this tutorial to get started with using Anypoint MQ to send messages to and retrieve messages from a queue, which is a temporary storage area for a message.

In this tutorial, you:

  1. Create a queue in Anypoint MQ.

  2. Send messages to the queue, verify that they arrived, and view the message payload.

  3. Configure a connected app and use the ID and secret to configure Anypoint Connector for MQ (Anypoint MQ Connector) version 4.x.

    To use connected apps with Anypoint MQ, you must use Anypoint MQ Connector 4.x for Mule 4. If you are using Anypoint MQ Connector version 3.x or earlier, configure a client app instead. For information, see Configuring Client Apps.

  4. Create a Mule 4.x app in Anypoint Studio 7.x, configure Anypoint MQ Connector, and test the connection.

  5. Optionally create a Mule 3.9 app in Anypoint Studio 6.x, configure Anypoint MQ Connector for Mule 3, and test the connection.

    Because Mule 3.9 and Studio 6.x are in Extended Support, we recommend that you use the latest versions of Mule and Studio.

Before You Begin

Before you begin this tutorial, you must have:

  • Familiarity with Anypoint MQ Glossary

  • An Anypoint Platform account

  • An Enterprise license

    Contact the MuleSoft Sales team to get a free trial.

  • Anypoint Studio installed on your system

  • A curl command line tool for testing

    This tutorial uses the HTTP Listener trigger to initiate Anypoint MQ Connector access and a curl command to submit REST API commands to the Anypoint MQ Broker API.

Step 1. Create a Queue

First, you create a queue, send messages to the queue, verify that the messages arrived, and view the message payload.

To create a queue:

  1. Sign in to Anypoint Platform and click MQ in the navigation menu.

  2. If you want to test the queue in Anypoint Studio 7.x, switch to any environment except Design.

    For example, click the environment button and select Production:

    Environment button
  3. Click Destinations:

    Destinations option and the Add icon
    1 Destinations navigation menu displays available queues.
    2 Add icon creates a new queue or exchange.
  4. Click the Add icon (Add icon in Destinations) to display the menu:

    Add icon menu options
  5. Select Queue.

  6. In the Create Queue window, enter myDemoQueue in the ID field:

    Create Queue window

    Queue names can contain up to 127 alphanumeric (a-z, A-Z, 0-9) and punctuation (. -) characters. They cannot contain spaces or other characters.

  7. If you want, specify any other queue settings.

    For information, see Create a Queue.

  8. Click Create Queue.

    Anypoint MQ creates the queue in the Destinations page:

    Destinations page with the new queue

You have successfully created a queue. Next, you send messages to the queue, verify that the messages arrived, and view the message payload.

Step 2. Send Messages to a Queue and Verify That They Arrived

After you create the queue, you test the queue sending messages to it, validating that the messages arrived in the queue, and retrieving them from the queue.

To send and receive messages from a queue:

  1. In the Destinations page, click myDemoQueue.

  2. Click Message Sender in the navigation menu:

    Message Sender navigation menu
  3. Leave the Type field set to Text and enter Hello Mules! in the Payload field.

  4. If you want, toggle Add User Properties, specify the property name-value pairs to send, and click Add Property:

    Add User Properties toggle in the Message Sender page

    The Message Sender page shows added string properties in blue and number properties in gray.

  5. Click Send.

  6. Set the Type to CSV, enter the following in the Payload field, and click Send:

    "dog",
    "fish",
    "parrot"
  7. Set the Type to JSON, enter the following in the Payload field, and click Send:

    {
    "animal that walks":"dog",
    "animal that swims":"fish",
    "animal that flies":"parrot"
    }

    Next, verify that the messages arrived in the queue.

  8. Click Destinations.

  9. Click the queue type for myDemoQueue to display its details.

    The details pane shows the number of messages in the queue:

    Three messages in the myDemoQueue queue in the details pane
  10. In the details pane, click the queue details label myDemoQueue.

  11. Click Message Browser in the navigation menu:

    Message Browser in the navigation menu
  12. Enter 5 for the number of messages to retrieve and 10 for the number of seconds to poll.

    You can retrieve between 1-500 messages and poll for between 1-3600 seconds.

  13. Click Get Messages.

    Anypoint MQ retrieves the messages from the queue and displays them in the Message Browser.

  14. Click the message row to view the message payload in the details pane:

    Selected message row and the payload in the details pane
    1 Selected message.
    2 Message payload in the details pane.

You have successfully created a queue in Anypoint MQ, sent messages to the queue, verified that the messages arrived, and viewed the message payload. Next, you create a connected app to connect a Mule app to Anypoint MQ.

Step 3. Configure a Connected App

After you test the queue, you configure a connected app, which generates the ID and secret you use to configure Anypoint MQ Connector.

To use connected apps with Anypoint MQ, you must use Anypoint MQ Connector 4.x for Mule 4. If you are using Anypoint MQ Connector version 3.x or earlier, configure a client app instead. For information, see Configuring Client Apps.

To create a connected app with Anypoint MQ scopes:

  1. Sign in to Anypoint Platform and click Access Management in the navigation menu.

  2. Click Connected Apps in the navigation pane.

  3. Click Create app.

    Connected Apps option and Create app button
    1 Connected Apps displays and creates connected apps.
    2 Create app button creates a connected app.
  4. Specify a name, such as DemoApp1 for the app.

  5. Select App acts on its own behalf (client credentials).

  6. Click Add Scopes.

    Scopes are roles with associated permissions, which determine which actions the app can perform within an organization and environment. See Anypoint MQ Scopes.

    Scopes for Anypoint MQ connected apps
  7. In Add Scopes window:

    1. Enter "MQ" in the Filter scopes field.

    2. Click Select all to select all scopes and click Next.

    3. Select a business group to apply the scopes to and click Next.

    4. Select the Production environment and click Next.

    5. Click Add Scopes.

  8. Click Save.

When you configure the connector in a later step, click Copy Id and Copy Secret to copy the ID and secret for DemoApp1:

Connected Apps window

You have successfully configured a connected app. Next, you create a Mule app in Anypoint Studio and connect it to Anypoint MQ.

Step 4. Create and Connect a Mule 4.x App to Anypoint MQ in Anypoint Studio 7.x

After you configure the connected app, use Anypoint Studio 7.x to configure an app that connects to Anypoint MQ. Then, deploy the app to CloudHub, submit a curl command to test the connection, and use Anypoint MQ to view the results.

Before You Begin

  • Install Studio 7.x.

  • In Anypoint Platform, change to any environment except Design.

    Ensure that the queue you created in Anypoint Platform > MQ is also in this environment.

Configure an App with Anypoint MQ in Anypoint Studio 7.x

First, use Studio 7.x to configure an app that connects to Anypoint MQ:

  1. Start Studio, click File > New > Mule Project, and then name the project mq-demo.

  2. Click the Exchange icon in the toolbar:

    Exchange icon in the Studio 7.x toolbar
  3. Enter your Anypoint Platform username and password.

  4. In Exchange, click the Provided by MuleSoft navigation menu, select Connectors from the drop-down menu, and then search for Anypoint MQ Connector:

    Provided by MuleSoft menu, Connectors drop-down menu, and search field in Exchange
  5. Select Anypoint MQ Connector - Mule 4.

    Studio 7.x supports Mule 4.x.

  6. Click Add to project:

    Add to project icon in Exchange
  7. At the confirmation prompt, click Proceed and then click OK in Studio.

  8. In the Mule Palette view, click HTTP, and then drag the Listener to the Studio canvas:

    HTTP Connector and Listener operations in the Mule Palette
    1 HTTP Connector displays the Listener operation.
    2 Listener operation identifies the operation to drag to the Mule Palette.
  9. Click Anypoint MQ and then drag Publish to the Process area for the Listener on the canvas:

    Publish operation in the Studio 7.x canvas
  10. In the canvas, click Listener.

  11. In the Listener properties window, set General > Path to /mq/{messageId}:

    Listener properties window
  12. Click the Add icon (Add icon in Studio) for the Connector configuration field.

  13. In the HTTP Listener Global Element Properties window, verify that the value of Host is All Interfaces [0.0.0.0] and Port is 8081, and then click OK:

    Host and Port settings in the Global Element Properties for the Listener

    The Listener properties window now indicates that there are no errors.

  14. In the canvas, click the Publish operation.

  15. In the Publish properties window, set Destination to the myDemoQueue queue that you created earlier in Anypoint Platform > MQ:

    Destination property and Add icon for the Publish operation

    The location of the queue must be an environment other than Design.

  16. Click the Add icon (Add icon in Studio) for the Connector configuration field to display the Global Element Properties window.

  17. Return to your Anypoint Platform > MQ browser window to copy the URL, ID, and secret values:

    1. Click Destinations.

    2. Click Copy and select Connector version 4.x and later from the menu to copy the URL:

      Copy button in the Destinations page

      If you are using Anypoint MQ Connector version 3.x or earlier, configure a client app instead. For information, see Configuring Client Apps.

    3. In the Anypoint MQ Config window in Studio, paste the URL in the URL field.

    4. Return to the Connected Apps window and copy the ID and secret for the app.

    5. Paste the ID and secret values to the Anypoint MQ Config window in Studio:

      URL, Client App ID, and Client Secret fields in the Anypoint MQ Config window
  18. Click Test Connection to ensure that the values are correct, and then click OK twice.

  19. Save your project.

  20. In Package Explorer in Studio, right-click mq-demo and select Anypoint Platform > Deploy to CloudHub.

  21. At the prompt, select the environment to which to deploy the app.

  22. In the Anypoint Platform Deploying Application window, click Deploy Application:

    Deploy Application button in the Deploying Applications page

You have successfully used Studio 7.x to configure an app that connects to Anypoint MQ. Next, you test the connection by submitting REST API commands to the Anypoint MQ Broker API and using Anypoint MQ to view the results.

Test Your Mule 4.x App in Studio 7.x

After you create the app in Studio 7.x, you test your app using a curl command to submit a REST API command to the Anypoint MQ Broker API. Then, use Anypoint MQ to view the results.

To test your app:

  1. In Anypoint Platform, select Runtime Manager > Applications and ensure that the mq-demo app is running.

  2. Click the status for the mq-demo app to display the details pane.

  3. Copy the App url value:

    App url value in Runtime Manager
  4. Run the following curl command from a command prompt to send the body value as an Anypoint MQ message.

    Replace APP-URL with the App url value that you copied from Runtime Manager.

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

    The 100 value in the URL is the message ID value as specified by /mq/{messageId} in the HTTP Listener, which is the identifier for your message.

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

    After making changes, redeploy your Mule app to CloudHub by right-clicking the project in Studio 7.x and selecting Anypoint Platform > Deploy to CloudHub.

  5. In Anypoint Platform > MQ > Destinations, click the name of the myDemoQueue queue that you created earlier and specified in the Destination setting for your app in Studio 7.x.

  6. Click Message Browser in the navigation menu:

    Message Browser in the navigation menu
  7. Enter 5 for the number of messages to retrieve and 10 for the number of seconds to poll.

  8. Click Get Messages.

    Anypoint MQ retrieves the messages from the queue and displays them in the Message Browser.

  9. Click the message row to view the message payload in the details pane:

    Selected message row and the payload in the details pane
    1 Selected message.
    2 Message payload in the details pane.
  10. Try changing the body text in the curl command and repeat the steps to view the message.

Example App XML for Studio 7.x

You can use this XML to create the mq-demo app in Studio 7.x.

Click Configuration XML tab in Studio and compare your XML to the following. Make corrections if needed.

<?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: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:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="0778dad7-d959-4396-8532-883a6c230501" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<anypoint-mq:config name="Anypoint_MQ_Config" doc:name="Anypoint MQ Config" doc:id="973e27ca-beff-4510-88d8-11a05053355b" >
		<anypoint-mq:connection url="https://mq-us-east-1.qax.anypoint.mulesoft.com/api/v1" clientId="CLIENT-ID" clientSecret="CLIENT-SECRET" />
	</anypoint-mq:config>
	<flow name="mq-demoFlow" doc:id="1e7b7e94-0759-4a2f-83f0-f425ca95b571" >
		<http:listener doc:name="Listener" doc:id="57828e3a-d5c0-4b94-ac82-84e2c0e4dd2e" config-ref="HTTP_Listener_config" path="/mq/{messageId}"/>
		<anypoint-mq:publish doc:name="Publish" doc:id="17060fa4-d043-4b2f-bc5b-ecc3c905f5a2" destination="myDemoQueue" config-ref="Anypoint_MQ_Config"/>
	</flow>
</mule>

Step 5. Create and Connect a Mule 3.9 App to Anypoint MQ in Anypoint Studio 6.x

If you use Mule 3.9, you can use Anypoint Studio 6.x to create a Mule 3.9 app similar to the app you created in Studio 7.x. In Studio 6.x, you can deploy the app to CloudHub and submit a curl command to test the connection.

This section applies only to Anypoint Studio 6.x and Mule 3.9. For Studio 7.x, see Step 4. Create and Connect a Mule 4.x App to Anypoint MQ in Anypoint Studio 7.x.

Because Mule 3.9 and Studio 6.x are in Extended Support, we recommend that you use the latest versions of Mule and Studio.

Before You Begin

  • Install Studio 6.x.

  • In Anypoint Platform, change to any environment except Design.

    Ensure that the queue you created in Anypoint Platform > MQ is also in this environment.

  • In Anypoint Platform, create a client app for authentication.

    Because Anypoint MQ Connector for Mule 3 doesn’t support connected apps, you must create a client app for authentication. See Configuring Client Apps.

    Keep the Client Apps window open so you can copy the client app ID and client secret in a later step.

Configure a Mule 3.9 App with Anypoint MQ in Anypoint Studio 6.x

To configure a Mule 3.9 app in Studio 6.x:

  1. Start Studio, click File > New > Mule Project, name the project mq-demo-studio-6, and then click Finish.

  2. Install the Anypoint MQ Connector:

    1. Click the Exchange icon in the toolbar:

      Exchange icon in the Studio 6 toolbar
    2. Enter your Anypoint Platform username and password.

  3. In Exchange, click the Provided by MuleSoft navigation menu, select Connectors from the drop-down menu, and then search for Anypoint MQ Connector:

    Provided by MuleSoft menu, Connectors drop-down menu, and search field in Exchange
    1. Select Anypoint MQ Connector - Mule 3.

      Studio 6.x supports Mule 3.9.

    2. Click Install:

      Install icon on the Exchange page
    3. At the prompt, check the items to install and click Next twice.

    4. Accept the license agreement and click Finish.

    5. At the prompt, click Restart Now to finish installing the connector.

  4. In the Mule Palette, drag HTTP to the Studio canvas:

    HTTP Connector in Mule Palette
  5. In the Mule Palette:

    1. Search for set, and then drag Set Payload to the Process area for HTTP on the canvas.

    2. Search for mq, and then drag Anypoint MQ to the canvas for Set Payload.

    3. Search for logger, and then drag Logger to the canvas for Anypoint MQ.

      Flow in Studio 6.x Canvas
  6. In the canvas, click HTTP.

  7. In the HTTP properties window, set General > Path to /mq/{messageId}:

    HTTP properties window
  8. Click the HTTP connector and click the Add icon (Add icon in Studio) for Connector Configuration.

  9. In the HTTP Connector Global Element Properties window, verify that the value of Host is 0.0.0.0 and Port is 8081, and then click OK.

    The HTTP properties window now indicates that there are no errors.

  10. In the canvas, click Set Payload.

  11. In the Set Payload properties window, set General > Value to:

    #[message.inboundProperties.'http.uri.params'.messageId]
    Payload in the Value field
  12. In the canvas, click Anypoint MQ.

  13. In the Anypoint MQ properties window, click the Add icon (Add icon in Studio) for Connector Configuration.

  14. Return to Anypoint Platform > MQ to copy the URL, client app ID, and client secret values to Studio:

    1. Click Destinations.

    2. Click Copy and select Connector version 3.x and earlier from the menu:

      Copy button in the Destinations page

      Use Anypoint MQ Connector version 3.x with Mule 3.9 and Studio 6.x.

    3. In Studio, in the Anypoint MQ Configuration window, paste the URL in the URL field.

    4. In Anypoint Platform > MQ, click Client Apps and copy the client app ID and client secret for the app.

    5. In Studio, paste the client app ID and client secret values to the Anypoint MQ Configuration window:

      URL, Client App ID, and Client Secret fields in the Anypoint MQ Config window
  15. Click OK.

  16. In the Anypoint MQ properties window, click the Operation field and select publish from the menu:

    Operation and Destination fields in the Anypoint MQ properties window
  17. Set Destination to the myDemoQueue queue that you created earlier in Anypoint Platform > MQ.

    The queue must be in an environment other than Design.

  18. In the canvas, click Logger.

  19. In the Logger properties window, set General > Message to MQ Message: #[payload]:

    Message payload in Logger properties
  20. Save your project.

  21. In Package Explorer in Studio, right-click mq-demo-studio-6 and select Anypoint Platform > Deploy to Cloud.

  22. At the prompt, select the environment to which to deploy the app.

  23. In the Anypoint Platform Deploying Application window, click Deploy Application:

    Deploy Application button on the Deploying Applications page

You have successfully used Studio 6.x to configure a Mule 3.9 app that connects to Anypoint MQ. Next, you test the connection by submitting REST API commands to the Anypoint MQ Broker API and using Anypoint MQ to view the results.

Test Your Mule 3.9 App in Studio 6.x

To test your Mule 3.9 app, follow the steps in [Test Your App] to use a curl command to submit a REST API command to the Anypoint MQ Broker API. Then, use Anypoint MQ to view the results.

Example App XML for Studio 6.x

After you configure your app 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:http="http://www.mulesoft.org/schema/mule/http" xmlns:anypoint-mq="http://www.mulesoft.org/schema/mule/anypoint-mq" 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" clientSecret="CLIENT_SECRET"/>
    </anypoint-mq:config>
    <flow name="mq-demo-studio-6Flow">
        <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" destination="myDemoQueue" doc:name="Anypoint MQ"/>
        <logger message="MQ Message: #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>