Nav

Lab 3: Leverage Anypoint MQ to broadcast Order Fulfillment updates via Notification API

Overview

When the Order Fulfillment API is invoked, we want to broadcast a notification event across multiple heterogeneous channels exposed as a System API. We will use Anypoint’s built-in Message Queueing system to provide the necessary one-way, publish and subscribe capabilities to enable our notification broadcast. Two additional flows will be created, each of which leveraging pre-built Notification APIs from Exchange 2.0 to rapidly construct our Notification flows.

module 9 Lab 3 pub sub

The implementation will consist of a few steps:

  1. Configure a publish and subscribe messaging pattern using Anypoints cloud messaging service: Anypoint MQ.

  2. Extend the Order Fulfillment API flow to publish a single Send Notification message to AnyPoint MQ.

  3. Leverage Exchange 2.0 to reuse Gmail/Twitter and Slack Connectors for creation of two Notification subscribers.

  4. Use DataWeave to manage data transformations across Notification API calls.

  5. Run and test the Mule app containing the Process API from Anypoint Studio.

Step 1: Configure Anypoint MQ

To get started, let’s configure Anypoint MQ and the underlying Queues or Destinations we’ll use to construct our publish and messaging pattern. By using a publish and subscribe pattern for this interaction, we can broadcast our Notification to many subscribers, in our case: Slack and Gmail/Twitter social channels.

  1. Let’s go ahead and open sign-in to Anypoint Platform, selecting MQ

    module 9 lab 3 anypoint mq
  2. Next let’s create the necessary Queue Destinations that will support our publish and subscribe notification pattern.

  3. Select Sandbox environment

    module9 lab3 select env
  4. Press + button in the right upper hand corner, then Select Queue

    module 9 lab 3 anypoint mq queue
  5. Let’s give our first Queue the name <username>-order-user-notification-queue

  6. Accept the default values for all other fields, choose Save Changes

  7. Create a second Queue, giving it the name <username>-order-slack-notification-queue

  8. Accept the default values for all other fields, choose Save Changes

    Your Queue definitions should resemble this illustration:

    module 9 lab 3 anypoint mq queue config
  9. Next we need to configure an AnypointMQ Exchange to enable the sending and receiving of messages to our recently defined Queues.

  10. Press + button in the right upper hand corner, then Select Exchange

    module 9 lab 3 anypoint mq exchange
  11. Let’s give our exchange the name <username>-order-exchange, followed by selecting each of the two Queues we created in the last step.

    module 9 lab 3 anypoint mq exchange config
  12. Choose Save Changes

  13. The final Anypoint MQ configuration is to create a Client App. By configuring a Client App, we can securely register an application with the Anypoint MQ host.

  14. Select the Client Apps link in the left side nav bar, followed by the + in the upper right hand corner of the page.

    module 9 lab 3 anypoint mq client apps
  15. Give the application a name, let’s call it <username>-order-notifications

  16. Choose Save Changes and wait a few seconds for the screen to refresh, showing the newly created client application registration. Once the application is registered successfully, it will be displayed with a Client ID as well as a Client Secret.

  17. Please take note of the ID and Secret, as they will be used in future steps within this lab.

module 9 lab 3 anypoint mq client app config

Step 2: Extend Process API to publish Notification events to Anypoint MQ

In this step we want to create a new branch in the Scatter-Gather and transform the message to prepare it for the successful asynchronous, invocation of the Notification API using Anypoint MQ.

Before we begin, we want to make sure that our Order Fulfillment flow reflects the below illustration:

module 9 Lab 3 pub sub

We are going to extend the flow after the Scatter-Gather and will enable us to properly mediate a Notification message and publish it to AnyPoint MQ.

The next step will be to create an example of the message that we are going to send through the exchange queue.

  1. Go to the folder src/test/resources and press right button.

  2. Select New → Folder

    module9 lab3 new folder
  3. Complete with the name examples in the Folder name

    module9 lab3 examples folder name
  4. On src/test/resources/examples create a new File called publish-message-example.json

  5. Copy and paste the following text:

    {
    	"order_id":"1",
    	"customer_data":{
    		"id": "b4285d5f-69c1-40fc-97b3-d112a6366b68",
      		"email": "test_email123@example.com",
    	  	"name": "Mike Prowl",
    	  	"phone": "1-(951)768-8479",
    	  	"shippingAddress": {
    	    	"address1": null,
    	    	"city": "Houston",
    	    	"country": "USA",
    	    	"postalCode": "77045",
    	    	"state": "Texas"
    	  	},
    	  	"billingAddress": {
    	    	"address1": null,
    	    	"city": "Riverside",
    	    	"country": "USA",
    	    	"postalCode": "92513",
    	    	"state": "California"
      		}
    	},
    	"products":[
    		{
    			"id": 3,
    			"productName": "Hoodie",
    			"quantity": 1
    		},
    		{
    			"id": 5,
    			"productName": "Mug",
    			"quantity": 2
    		}
    	]
    }
  6. Save and Close the example file.

  7. Now, add the Anypoint MQ Component to the palette.

    To import the Anypoint MQ Connector, you need to go to Exchange.

  8. Go to the palette and press Search in Exchange

    module9 lab3 search exchange
  9. In the search box enter Anypoint

  10. Select the Anypoint MQ Connector

  11. Press the Add> button.

    You should see the connector on the Selected modules pannel

    module9 lab3 selected connector

    Be sure it’s version 2.1.0 or later.

  12. Press Finish

    You should see the Anypoint Connector on the Connector Palette.

    module9 lab3 anypoint mq module
  13. Remove the last Transform.

  14. In the Anypoint MQ Palette, Drag and Drop the Publish icon after the Scatter Gather component.

    module9 lab3 add publish
  15. Click on the Publish Icon

  16. Configure the Anypoint MQ client by creating a new Connector Configuration.

    Copy and paste in your Client ID and Client Secret from the MQ Client created on Step 1.

    module9 lab3 mq connection config
  17. Press OK

  18. Under the General section

    Complete with the following table:

    Key Value

    Destination

    <username>-order-exchange

    Body

    #[payload]

    Properties

    None

    module9 lab3 mq config

    Now we are going to Map the message that we are going to send through the Anypoint MQ.

  19. Click module9_lab2_transform_button

  20. Click Define metadata link

    module9 lab3 define metadata

    A new window will open

  21. Press Add button to create a new data type.

  22. Complete with the name publish_message_example and press create type

  23. Select JSON in the type drop down list

    module9 lab3 metadata

    New fields to be completed will appear.

  24. Select example from the drop down and write examples/publish-message-example.json as the path.

    You also can navigate to the file.

    module9 lab3 metadata selected
  25. Press Select

  26. Save the project

    Now that the Metadata was set, we can continue to map the Body.

  27. Select publish-message-example from the User Defined window.

  28. Press Select when finished.

    Now that we can see that the output datasense . We need to map a new message to be sent to the Publish component.

  29. Replace the actual transformation with this one:

    %dw 2.0
    output application/json
    ---
    {
    	order_id: vars.order_id as Number,
    	customer_data: {
    		id: payload."1".payload.id,
    		email: payload."1".payload.email,
    		name: payload."1".payload.name,
    		phone: payload."1".payload.phone,
    		shippingAddress: {
    			address1: payload."1".payload.shippingAddress.text,
    			city: payload."1".payload.shippingAddress.city,
    			country: payload."1".payload.shippingAddress.country,
    			postalCode: payload."1".payload.shippingAddress.postalCode,
    			state: payload."1".payload.shippingAddress.state
    		},
    		billingAddress: {
    			address1: payload."1".payload.billingAddress.text,
    			city: payload."1".payload.billingAddress.city,
    			country: payload."1".payload.billingAddress.country,
    			postalCode: payload."1".payload.billingAddress.postalCode,
    			state: payload."1".payload.billingAddress.state
    		}
    	},
    	products: payload."0".payload.products map ( orderLineItem , indexOfOrderLineItem ) -> {
    		id: orderLineItem.id as Number,
    		productName: orderLineItem.productName as String,
    		quantity: orderLineItem.quantity as Number
    	}
    }
    module9 lab3 transform payload
  30. Save the project

    We don’t want to loose the input payload, so we are going to save the publish output into a variable.

  31. Go to Advanced.

  32. In the Output section, set the following values:

    • Target Variable: result

    • Target Value: #[payload]

      module9 lab3 target value

      Finally, we are going to generate the response

  33. Add a Transform component after the publish component.

  34. Complete the transform with the following dataweave script:

    %dw 2.0
    output application/json
    ---
    {
    	order_id: vars.order_id,
    	username: vars.username
    }

    That completes all the necessary steps to publish a Notification message to Anypoint MQ. Your final flow should resemble this:

module 9 lab 3 final flow

The next step will be the creation of two new flows that will subscribe to our Queues and trigger Notification events on both Twitter and Slack channels.

Step 3: Construct new Flows for Slack and Twitter Notification subscribers

In order to subscribe to Notification events that our Order Fulfillment flow will publish we’ll create two new flows within our Mule project to represent the Slack Notification channel, as well as Gmail/Twitter channel.

Each flow will send a copy of the same Notification message from our Process API, these Notification flows will consume the message and invoke the configured social endpoints that are part of the pre-built Notification (System) API.

  1. Create a new Mule Configuration file to encapsulate our Notification flows.

    module 9 lab 3 new mule config subscriber flows
  2. Name the new configuration file Notification Subscribers

  3. Drag a Subscriber Anypoint MQ component on the blank canvas to begin the construction of our first flow aimed at the Gmail/Twitter channel

  4. Rename the Flow name to notification-user-listener

  5. Rename the component to Twitter Notificaton Subscriber .

  6. Reuse the Anypoint MQ Connector Configuration object.

  7. Configure the Destination to <username>-order-user-notification-queue.

    Your configuration should resemble something similar to the below:

    module 9 lab 3 twitter notification flow
  8. Press the save button.

    We are going to define the Message that we are going to receive. Remember that is the same message we are publishing.

  9. Go to the Metadata tab

    module9 lab3 subscriber metadata
  10. Press Add metadata button

  11. Select Output: Payload and press the edit button

    module9 lab3 subscriber metadata edit
  12. Select metadata publish_message_example and press Select

    module9 lab3 suscriber select message

    Now that we set the metadata for that component. Is going to be easier for Anypoint Studio to manage the datasense.

    We said that we were going to consume the Notifications API. So we need to add that connector.

  13. Go to Add Module and search for Notification API

    module9 lab3 notification api module

    After Pressing Finish, the Notification API module will be ready to be used in the palette.

    module9 lab3 notification api palette
    The connector version can change. Be sure that is the last version.
  14. Drag and Drop Create id by id resource.

    module9 lab3 create by id resource
  15. Double-click on the connector to display its properties. You should see the component properties for you to configure in the Mule Properties View.

  16. Click on module9_lab2_add_button icon to create a new configuration as shown:

    module9 lab3 notification api config
  17. Complete the Request Configuration

    • Host: mythical-notification-api-v4-0.cloudhub.io

    • Port: 80

    • Base Path: /api

    • Protocol: HTTP

      Notice the Host value can be configured in the configuration.yaml file So we can change the value to ${system.api.host.notification}
      module9 lab3 notification api configuration
  18. Click OK

  19. In the General section, set the following parameters:

    Name Expression Value

    Id

    #[payload.customer_data.id]

    Create id by id request data

    #[payload]

    Client id

    11def1b704e24d87a5ea5769772c90a7

    Client secret

    88845E529f1F42E4a5aC96Fd504c3e01

    Content Type

    application/json

    Accept

    application/json

    The configuration should look similar to this:

    module9 lab3 twitter notif config

    Let’s complete the request data.

  20. Press module9_lab2_transform_button and add the below DataWeave logic into the code editor:

    %dw 2.0
    output application/java
    var customer=payload.customer_data.name default ""
    var order_id=payload.order_id default 0
    ---
    {
    	body: "Thank you " ++ customer ++ ", your order (#" ++ order_id ++ ") has been received.",
    	subject: "Order " ++ order_id
    }

    You should see something similar to this:

    module9 lab3 notification user dw
  21. Finally add a Logger component. This will output an intuitive log entry indicating that the Notification was sent.

  22. In the Message put the following string: Notification to Twitter successfully Sent

    module 9 lab 3 twitter notification logger

    The Flow should look similar to this

    module9 lab3 notification user listener final flow
  23. Save your flow configuration.

The next flow that we’ll configure for this step will be our Slack Notification Flow.

  1. Drag another Subscriber Anypoint MQ component onto the canvas. This will create a new flow.

  2. Rename the name of the flow to notification-slack-listener

  3. Rename the component to Slack Notificaton Subscriber .

  4. Reuse the Anypoint MQ Connector Configuration object.

  5. Configure the Destination to <username>-order-slack-notification-queue.

  6. Supply the correct name of the Anypoint MQ Queue destination. Your configuration should resemble something similar to the below image:

    module 9 lab 3 slack notification anypoint mq
  7. Go to the Metadata tab

  8. Press Add metadata button

  9. Select Output: Payload and press the edit button

  10. Select metadata publish_message_example and press Select

    Again we are setting the output metadata so is going to be easier for Anypoint Studio to manage the datasense.

  11. Add the component Create slack from the Notification API connector

  12. Reuse the Connector configuration already created.

  13. In the General section, set the following parameters:

    Name Expression Value

    Create slack request data

    #[payload]

    Client id

    11def1b704e24d87a5ea5769772c90a7

    Client secret

    88845E529f1F42E4a5aC96Fd504c3e01

    Content Type

    application/json

    Accept

    application/json

    The configuration should look something like this:

    module9 lab3 slack notif config

    Let’s complete the request data.

  14. Press module9_lab2_transform_button and add the below DataWeave logic into the code editor:

    %dw 2.0
    output application/json
    ---
    {
    	userId: payload.customer_data.id,
    	productList: payload.products map ( product , indexOfProduct ) -> {
    		qty: product.quantity,
    		name: product.productName
    	}
    }
    module9 lab3 notification slack dw

    Press the module9_lab2_done_button button.

  15. Finally add a Logger component. This will output an intuitive log entry indicating that the Notification was sent.

  16. In the Message put the following string: Notification to Slack Successfully Sent

    Your logger configuration and final flow should reflect the below illustration:

    module 9 lab 3 slack notification logger

    The Flow should look similar to this

    module9 lab3 slack transform step
  17. Save your flow configuration.

Step 4: Run Process API to simulate Order Fulfillment and Anypoint MQ Notifications

Now it’s time to run the API in Anypoint Studio to see how it all works together.

  1. Start the application as done in the previous Lab. (Right-click the application, RUN AS > Mule Application

  2. Once the API Console View opens in Anypoint Studio, click on the POST button on the /orders_fulfillment resource.

  3. Under Try it, view the request body then click on the POST button below.

    module9 lab2 api console post button fulfillment
  4. Once the request is served, you should see a 200 response code and the JSON response of our API.

  5. Review GMail, Twitter and/or Slack channels with the Instructor to view the broadcasted Notification message.

    module9 lab2 api console post success response

    You can download a complete project from exchange.

Summary

In this module you completed the following steps:

  1. Using Anypoint Studio, implemented the Order Fulfillment Stub to complete the Process API layer.

  2. Configured the Scatter-Gather flow control and RAML Consumer objects to orchestrate Order and Customer System APIs.

  3. Using DataWeave, managed System and API responses to produce desired result set.

  4. Tested the Process API implementation that traverses the System API layer using live API calls from RAML Consumers.

For further reading on components that we used in this lab, please refer to the following documentation:

Congratulations! You have completed lab 3, the final of Module 9.