How To Use JMS ActiveMQ With Mule 4 - Part 6
In this blog, we will see how we can use Durable topics to ensure that no message is lost in case the subscriber goes offline.
Join the DZone community and get the full member experience.
Join For FreeThis is the 6th part of the JMS ActiveMQ with the Mule 4 series. You can read previous parts here: Part 1, Part 2, Part 3, Part 4, Part 5 In this post, we will learn about the durable topic and how we can use it in MuleSoft.
JMS topics are used to implement a pub-sub model where the same message is broadcasted to multiple subscribers. In simple words, A message published to a topic can be consumed by any number of subscribers. But a topic subscriber can only pick the message if it listens to the topic at the time of publishing of Message means the subscriber will miss the message if not listening to the topic.
We can ensure that the topic subscriber can pick the messages that were published when the subscriber was not listening to the topic or was inactive/offline. This could be done by using Durable Topic.
A durable topic ensures that a message would be delivered to the subscriber. If the subscriber is active and listening to the topic then message would be delivered instantaneously, but if the subscriber is offline and not listening to the topic when the message is published then the message would be stored by JMS broker and once the subscriber comes online, all the stored messages would be delivered.
We can understand how this works with the help of below example.
We will create a mule flow where we have On New Message Listener which is pointing to the topic and a logger that will print and displays the received messages. In the configuration, we selected Consumer type as Topic, Marked Durable and provided a unique subscription Name.
JMS configuration is done as shown below, where we are giving a unique client Id for JMS connection along with username, password, broker url.
Note: JMS identifies a durable topic subscriber by combination of clientID and durable subscriber name.
We can test the connection, after filling the required fields. If JMS is running then we will get a successful connection.
So, we are good with our mule flow and config. Now lets have a look at the JMS console. We can see my_topic1 in the list.
On clicking, at subscribers we can see that there are no subscriber present at the moment for JMS.
To make our Durable topic subscriber work, we need to first register subscriber with JMS. This would be done automatically when we run our Mule application.
After running mule application, we can see that there is an active subscriber in the Active Durable Subscribers list. We can also see that it has same client ID (myClient1) , Subscription Name (my_subscription1) and topic (my_topic1) that we configured in our mule flow.
Lets try to send a message into topic and understand how it works when subscriber is active. For this we will add the message content in Message Body section and click on Send.
As the mule app is running, this message would be immediately picked by On New Message Listener in our mule flow and processed. We can see the details for same in below mule logs:
********************************************************************** INFO 2021-07-29 19:11:15,663 [[MuleRuntime].uber.01: [jms-demo].jms-durable-demoFlow1.CPU_LITE @7ada34fb] [processor: jms-durable-demoFlow1/processors/0; event: ] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: "Recevied message is : " { "Message" : "This is a test" } |
On JMS Console we can see that my_topic1 has 1 subscriber and 1 message has been published and successfully delivered.
On switching to Subscribers tab, We can see that the subscriber is in active state and it has successfully delivered 1 message and there is no pending message to be delivered.
Now we will stop our mule app and try to publish a new message into the topic from JMS console. After stopping mule app, if we look at JMS subscriber tab we can see that our subscription is now moved to Offline Durable Topic Section.
Now, we will try to publish below message into topic my_topic1.
On sending the message, when we see JMS topic we can confirm that there are 2 messages published into the topic.
On the Subscriber tab, we can see that our subscriber is now moved into the offline Durable Topic Subscriber Section and there is 1 message in Pending Queue Size column. This is the same message that was just published, and as Subscriber is offline, it is stored.
Similarly, all the message that would be sent, when subscriber is offline, are going to be stored and seen under column Pending Queue Size.
On starting the mule api again, firstly we will receive the message which was published into the topic when the subscriber was offline. We can see the details for same in below Mule logs:
INFO 2021-07-29 19:30:36,408 [[MuleRuntime].uber.01: [jms-demo].jms-durable-demoFlow1.CPU_LITE @72a5b6b0] [processor: jms-durable-demoFlow1/processors/0; event: ] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: "Recevied message is : " { "Message" : "This message is sent when subscriber is offline" } |
On looking at JMS Topic we can confirm that there are 2 messages published into the topic and both are delivered to the subscriber.
When we have a look at Subscribers tab, we can see that Subscriber has been moved to Active Durable Topic Subscriber section and there is 0 messages pending and total message processed count is updated to 2.
In this way we can use Durable Topic subscription and ensure that no message is loss when subscriber is inactive or offline.
Opinions expressed by DZone contributors are their own.
Comments