Author: Suraj Rohankar
The Anypoint MQ APIs allow you to use REST to publish and consume messages, administer queues and message exchanges, and analyze results.
Anypoint MQ provides these APIs:
- Anypoint MQ Admin API
- Anypoint MQ Broker API
- Anypoint MQ Stats API
Anypoint MQ Admin API:
- The Anypoint MQ Admin API provides access to Anypoint MQ administrative functionality.
- You can use the Admin API to create and manage apps, queues, and message exchanges.
Anypoint MQ Broker API:
- The Anypoint MQ Broker API allows you to Publish, Consume, ACK, NACK messages from queues and exchanges.
- For Broker API, we need access token, environment Id, and organization Id. After calling Authorize API, we can get these values.
- The access token in this section can only be used with the Broker API. Do not use this access token with the Stats and Admin APIs.
Anypoint MQ Stats API:
- The Anypoint MQ Stats API provides access to statistical information about Anypoint MQ message queues.
- Live statistics allow you to view real-time data, including the number of messages currently in a queue.
- You may also view historical data to know queue activity over time.
- You can also view your entire organization’s usage of MQ to get a clear picture of how much you are consuming the service on a monthly basis for billing purposes.
In this article, I will walk you through the Anypoint MQ Broker API.
- Publish message to a queue.
- Consume a message from a queue.
- Send acknowledge signal to the queue (It means the consumer successfully consumes the message, and the message will automatically delete from the queue).
- Send a negative acknowledge signal to the queue (It means the consumer does not consume the message. the Picked message will return to the queue).
This article assumes that you are familiar with Anypoint Studio, Connectors, Dataweave, REST call using Request connector.
Let’s get started!!
Step 1: For accessing broker API we need an access token, an envId, and an orgId. First of all, we have to call authorization API.
This API call returns authorization access.
We will store this authorization payload into ObjectStore. So that we can retrieve wherever we need that.
Step 2: Now we have authorization access. We are good to use the Broker API.
- Set Variable: While using Anypoint MQ Broker API, we have to create message Id for each messages (It does not support automatic message Id creation)
|“ab1cd2ef3gh4ij5k” ++ randomInt(10000)|
- Retrieve: We are retrieving access payload by passing the key name. Later save this payload in the target variable.
- Transform Message: Make sure that you are sending a message as a string with body parameters.
- Request: We are about to call the Broker API for publishing messages to AnypointMQ.
Navigate to postman and send request PUT http://localhost:8081/publish
Navigate to Anypoint MQ and you should be able to see that our message has been successfully inserted into the queue.
Step 3: Create a flow for consuming published messages, acknowledge a message or negatively acknowledge a message as shown below.
- Acknowledging a message (ACK) informs the Anypoint MQ broker that the message has been processed and must be removed from the queue to prevent redelivery.
- Not acknowledging a message (NACK) tells the broker to not process the message and to return the message to the queue for redelivery to any available consumer.
- Request(GET Message): In this API call we will be retrieving the messages from Anypoint MQ.
- Request(ACK): In this API call we will acknowledge the message. So that message will get cleaned from the queue if the given condition is true.
- Request(NACK): In this API call we will send a negative acknowledgement. So that message will remain available in the queue for other consumers.
- If your given condition does match and returns true, the message won’t be available in the queue anymore and vice-versa. (If you are using Dead Letter Queue, messages will be stored in Dead Letter Queue in NACK scenario).
Conclusion: In this way, we can make use of the Anypoint MQ message broker. We can use these APIs in Mule applications but there are some limitations to it. The better way is to go for an Anypoint MQ connector for Mule applications. If non Mule platforms wish to use Anypoint MQ message broker they can easily consume these API’s.
You can check out source code over here or simply login to anypoint exchange from anypoint studio and open this application as an example by using below credentials.