Anypoint MQ Connector

  • The Anypoint MQ provides publish-subscribe messaging to Mule apps using the Anypoint MQ cloud service.
  • This blog includes a demo for 4 operations: Publish, Subscriber, Ack, and Nack, provided by Anypoint MQ Connector.
  1. First, we will download the Anypoint MQ connector for that click on search in exchange. Login to your Anypoint account and select Anypoint MQ connector.
  • It is used to send a message to MQ.
  • This will publish the message given in the body into MQ Mentioned in Destination of config.
  • Create a simple flow as shown in the figure with publish config:
  • With postman send the request as follows:
  • If you go to Anypoint platform>MQ> mq-demo in the message browser, you can see published a new message.
  • It is used to read/retrieve messages from the MQ.
  • Subscriber will subscribe from the MQ mentioned in the Queue option of configuration.
  • It will prefetch 30 messages as per the above config in the local buffer, and the messages appear in in-flight. You can change that to any number as per your requirement.
  • Even If multiple messages are fetched at a time, it will still process one message at a time.
  • Default acknowledgment mode.
  • If the flow execution finishes successfully without any exception, it automatically sends the acknowledgment back, and the message is deleted from MQ.
  • If the flow execution finishes with a propagated exception, the message is automatically not acknowledged and returned to the redelivery queue.
  • The message is acknowledged as soon as it is fetched from the MQ local. It will not wait till the flow processes it.
  • In this mode, ack or Nack is sent to Subscriber manually using application logic.
  • Ack: It informs the Anypoint MQ that the message has been processed successfully and removes that message.
  • Nack: It informs the Anypoint MQ that the message is not processed successfully, so return the message to the queue for redelivery.
  • You will need the value of ackToken, which is provided as part of the message attributes.
  • Add a logger to print messages subscribed from MQ, and here the subscriber config is the default, as shown in the above screenshot.
  • As the subscriber type is prefetch as soon as the app is deployed it will fetch all messages from mq and try to process them one by one. As the acknowledgement mode is Auto it will wait for Automatically send acknowledgement as the flow processed successfully. As flow executed successfully it will delete the message from queue. If flow throws any exception, it will keep that message in queue as it will not receive Nack and if we have added any DLQ for this MQ, it will be sent to that DLQ. In our case it will be deleted from the queue as processed successfully without any exception.
    After deploying you can see that message we stored in previous published operation is subscribed from mq-demo as follow: Subscriber will be dropped in the source section.
  • Add a logger to print messages subscribed from MQ. Added a request component that will throw an error, and here the subscriber config is with Immediate Acknowledgement mode.
  • As the subscriber type is prefetch as soon as the app is deployed it will fetch all messages from mq and try to process them. As the acknowledgement mode is Immediate, as soon as it receives a message it will immediately send acknowledgement back and the message will be deleted from MQ whether it will process successfully or not.
    After deploying you can see that message will deleted from MQ and will not be sent to mq-demo-DLQ even if its not processed successfully as follow:
  • Using this operation, we can send an acknowledgment back that message is processed successfully when acknowledgment mode is manual.
  • Create a flow with the following 3 components with acknowledgment mode in subscriber config is manual and type is prefetch.
  • In ack, use ack token, which is present in input to Ack operation in attributes, and you can access that with attributes.ackToken.
  • As the subscriber type is prefetch, as soon as the app is deployed, it will fetch all messages from MQ and process them one by one. As the acknowledgment mode is manual, it will wait for manual acknowledgment from the flow. As soon as the Ack operation is executed successfully, it will delete the message from the queue. In the case before Ack operation flow throws an exception, it will keep that message in queue if no DLQ is added for this MQ or sent to DLQ as it will not receive any acknowledgment. In our case, it will be deleted from the queue as processed successfully.
  • Using this operation, we can send an acknowledgment back that message is not processed successfully when acknowledgment mode is manual.
  • Create a flow with the following 4 components with acknowledgment mode in subscriber config is manual and type is prefetch.
  • In Nack, use ack token, which is present in input to Ack operation in attributes, and you can access that with “attributes.ackToken”.
  • As the subscriber type is prefetch as soon as the app is deployed it will fetch all messages from mq and try to process it one by one. As the acknowledgment mode is manual it will wait for manual acknowledgment from the flow. As soon as Nack operation is executed successfully, it will keep the message in queue if no DLQ is added for this MQ or sent to DLQ as it will not receive a negative acknowledgment. As it’s a negative acknowledgment. In our case, it will be added to DLQ.
  • As in our case, mq-demo has DLQ, you can check that message in mq-demo-DLQ.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store