Saturday, September 30, 2023

Anypoint Message Queue - MQ

What is Anypoint MQ?

A queue is a temporary storage area, being used for message transformation from one API/service to another. If API-led connectivity approach, we may require a mechanism to send messages from our experience API to the process API so a queue can be used. Please note that there are many such use cases where queues can be used.

  • Anypoint MQ is a cloud messaging service that performs asynchronous messaging scenarios between applications such as queuing and pub/sub.
  • It's easily managed (create, delete) within Anypoint Platform.
  • Well supports huge payloads, encryption, queuing, and publish/subscribe patterns.
  • Enables easy connection to non-Mule applications using the REST API.
  • Provides Dead-Letter Queue (DLQ) for undeliverable messages.
  • Displays usage statistics like no of messages and API requests..etc
Note: Anypoint MQ is not included in the trial account. 
          We need a separate Enterprise subscription to use Anypoint MQ.

Anypoint MQ is only available for the Cloudhub-hosted application; it is not available for the APIs hosted on on-premise server.

Operations:

There are five operations available in Anypoint MQ connector (at the time of writing this blog):
  1. Publish - To publish a message to the Queue.
  2. Subscriber - To subscribe to the queue and listen for the receiving message.
  3. Consumer - To consume a message from the queue.
  4. Ack -To send an acknowledgement to the queue about the successful consumption of the message and delete the message from in-flight status.
  5. Nack - To send negative acknowledgement to the queue stating the consumed message is not processed successfully, changes the status of the message from in-flight to in-queue to be consumed again by an available consumer.
How to configure a Queue?
Once we have the license, we need to login to the Anypoint Platform where we can see MQ ..Click on MQ.

Select the region from drop down, which is near to your Workers 


Click on + button, select Queue, FIFO or Exchange based on your business use case..
  • Queue: Its a storage area where messages are stored for processing.
  • FIFO (first in, first out): FIFO is also a normal queue where messages are stored. The FIFO queue needs to be used when ordering is important. Whereas in the queue, ordering is not maintained.
  • Exchange: The exchange is used for broadcasting a messages, like one message to multiple consumers. If any message is send to the exchange, it will be send to all the the queues associated with the exchange.
In this example, I am selecting Queue as storage and specify ID (name), TTL and Default Ack Time out parameters and click on Create Queue button.


On the right hand side, you can see Queue statistics.

We need to create client apps to access the queue, which will provide us with a unique client ID and password 
Click on Client Apps and click on + and then specify App name. On the right hand side you can see Client ID/ Client Secret. Which are required to connect from Anypoint Studio - connector configuration.


 
How to Publish & Subscribe.?
Open Anypoint Studio and add Anypoint MQ Connector from the exchange, as it is not present in the Mule palette by default.


Once you have downloaded, you can see operations in palette


There are five operations available in Anypoint MQ connector (at the time of writing this blog):
  1. Publish - To publish a message to the Queue.
  2. Subscriber - To subscribe to the queue and listen for the receiving message.
  3. Consumer - To consume a message from the queue.
  4. Ack -To send an acknowledgement to the queue about the successful consumption of the message and delete the message from in-flight status.
  5. Nack - To send negative acknowledgement to the queue stating the consumed message is not processed successfully, changes the status of the message from in-flight to in-queue to be consumed again by an available consumer.
Create a Publish API
Drag publish operation onto canvas and configure connection, you already have the below details from Client Apps Step
Specify URL:  XXXXXXXXXXXXXXXXXX
Client ID: XXXXXXXXXX
Client Secret:  XXXXXXXXXXX


Test the connection and click on OK.

Subscriber API
Here, Subscriber is the source that enables the app to listen for new messages and consume them as they arrive at the destination and it's in continuous listening mode.
Default Subscriber Type is Prefetch. Other option is Polling, where you can specify polling frequency.

Consumer Operation
Here, we need to trigger the consumer through HTTP listener or any other way. So it will fetch available message (one at a time). If you want again, you need to trigger the flow again.


What is a Dead Letter Queue (DLQ)?
A dead letter queue is used to store the undelivered messages to the destination or target system. 

There might be cases where we want to make sure that in case of any failure, our message won’t get lost. In such cases, Dead Letter Queue (DLQ) can be used. A dead letter queue is associated with one or more queues, which are called the parent queue.

There are a few points to remember before creating DLQ. 
The DLF and parent queue both need to be:
  1. Same type of queue (Standard or FIFO)
  2. Created in the same geographical region
  3. Created in the same environment and owned by the same Anypoint Platform account
The process of creating a DLQ is same as normal queue. So, we create a DLQ and assign it to another queue (Parent).

Now, assign this queue as a DLQ to Parent Queue..(actual Qs, pointing/listening by applications)




Note: If you would like to process messages, which are in DLQ, you need to build a separate consumer logic. In some cases, you can purge those messages but discuss with your business owner before purge it.

Thanks for reading :-)

No comments:

Post a Comment