Wednesday, December 5, 2018

Spring Boot and ActiveMQ

In this post we will send a message to ActiveMQ when a new customer is registered in our Spring Boot app.

First we need to add the following dependencies to our pom.xml.


Next is to configure our messaging service. Why do we need this? First, because we want to encrypt our ActiveMQ password. Second, because we want to be able to switch between topic and queue. Here we create a new configuration class as shown below, separated from other configuration classes, for clarity purpose only.


We annotate the class with @Configuration to indicate that this is a Spring configuration class and @EnableJms to indicate that we want to enable JMS listener.


We have five private variables as shown below. The url, username, and password are self explanatory. Here we encrypt the password and later need to decrypt it. The trust-all flag is needed by ActiveMQ to indicate that all classes are trusted to be consumed. In our case, we will send our Customer class to be consumed. The flag isQueue indicates if we want to use either topic or queue. We will see this later. Those values are pulled from application.properties file.



Then we also define a method to decrypt the ActiveMQ password in application.properties file.


We create an ActiveMQ connection factory bean using the private variables we defined above. Note that we do this since we want to encrypt the ActiveMQ password in application.properties.


We then create a JMS template bean as shown below. Here we get connection from our previous ActiveMQ connection factory. We also set pubSubDomain flag based on isQueue flag in application.properties. PubSubDomain flag needs to be set to true if we want to use topic. Default value is false for queue.


We create a JMS listener container factory bean as shown below. Here we get our previously created ActiveMQ connection factory. We also set its concurrency to be min 1 and max 10 for queue. This means there will be min 1 consumer and max 10 consumers created. Note that a message in a queue is removed after a consumer consumes it. This means even though we have 10 consumers, only one consumer can consume it.
The concurrency is set to be min 1 and max 1 for topic. This means there will be only 1 subscriber created. Since the characteristic of a topic is broadcasting message, we should have max 1 subscriber for this topic. If not, the same message will be processed many times.
As we have seen in previous JMS template bean, we also set pubSubDomain flag to be true if we want to use topic and false if want to use queue.
Another thing worth noting is durable subscriber. Since the characteristic of a topic is broadcasting, a subscriber will not receive a message if the subscriber is inactive when the message is being broadcast. This is the main difference between topic and queue. Queue will retain the message until a consumer consumes it. To make an inactive subscriber get the message we can use durable subscription. If a topic has durable subscriber, the message is retained until all durable subscribers read it. In our code, we set client id and subscription durable flag to be true to create durable subscription.


Now we will see the actual classes that send and receive messages. The class that sends message is annotated with @Component to indicate that this is a Spring component class. Next we autowired previously created JMS template bean. This bean responsible to send our Customer object to mq.request queue or topic depending on our setting in application.properties.


The class that receives message is shown below. This class is also annotated with @Component to indicate that this is a Spring component class. We annotate the receive() method with @JmsListener and define the destination, the same destination where we send the message. Note that the message we send and receive is a Customer object.


Now we will send the message from CustomerController.addCustomer() method as shown below. We call method send() previously created in MessageSender class.



We will see the result of setting isQueue flag to false which means we want to use a topic.


Start ActiveMQ and start our application. Open ActiveMQ admin console and see if our topic "mq.request" is created and there is an active durable subscriber with id "durableSubscriberRHS" listed and listening on that topic. Note that there is only one subscriber created since we set max concurrency to be one.



Sending the message will make the Customer object printed on application console.


Shutting down our application makes the durable subscriber moved from active to offline as shown below. If we don't set it as durable, the subscriber will be removed on application shutdown.


Next we will set the isQueue flag to true.


Start our application and see if the queue "mq.request" is created and a consumer is listening on that queue.



Now we will send a message to that queue and the Customer object will be printed to console.


There are two active consumers now for our queue. This happens because we set max concurrency to be 10. ActiveMQ gradually create more consumer when it thinks it is needed.


1 comments:

abhaytabachnick said...

Slots Casino | Mapyro
Find 서울특별 출장안마 your nearest 양산 출장안마 Casino Slots Casino at Mapyro. 전주 출장마사지 Find your nearest Casino Slots Casino with Mapyro's directional directional directions. Save big 울산광역 출장샵 with our 전라북도 출장안마

 

©2009 Stay the Same | by TNB