Integrate embedded Apache ActiveMQ 5 (Classic) JMS Broker with Spring Boot application
[ Apache ActiveMQ
] Apache ActiveMQ is the most popular open source JMS server. ActiveMQ supports Spring for configuration of the JMS client side as well as for configuring the JMS Message Broker.
In this post, let’s integrate an embedded ActiveMQ JMS 5 broker (Classic version as they call it) with a simple Spring Boot application which sends/recieves data from the embedded ActiveMQ broker.
Table of contents
- Create spring boot starter application
- Configure additional project dependencies in pom.xml
- Configure Embedded ActiveMQ Broker
- Configure JMS Producer
- Configure JMS Consumer
- Test JMS producer and consumer
Create spring boot starter application
Building the bare bone Spring Boot Service is simple when Spring Initializr
is used. Spring Initializr
generates spring boot project with just what you need to start quickly! Let’s start off with one.
Create a Spring Boot starter project using Spring Initializr
Let’s utilize the pre-configured Spring Initializr
which is available here to create embedded-activemq-5-jms-broker starter project.
Click on Generate Project. This downloads a zip file containing embedded-activemq-5-jms-broker
project. Import the project to your IDE.
Configure additional project dependencies in pom.xml
We need an additional activemq-broker
dependency which will give all the classed needed for setting up an embedded ActiveMQ JMS 5 broker.
Uptate pom.xml with below dependencies. Here we are adding activemq-pool
dependency as well to create a pooled connection factory.
pom.xml
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
Let’s add broker URL in application.properties. In this guide, we shall embed an active mq broker to run on port 61616. We also need a queue to send/recieve messages. We shall configure this queue name as well here.
src/main/resources/application.properties
activemq.broker.url=tcp://0.0.0.0:61616
activemq.queue.name=my-queue-1
Configure Embedded ActiveMQ Broker
Create an Active MQ embedded broker as shown below. This broker will be started when the application starts up.
com.codeaches.activmq.embedded.JmsConfig.java
@Configuration
public class JmsConfig {
@Value("${activemq.broker.url}")
String brokerUrl;
@Bean
public BrokerService broker() throws Exception {
BrokerService broker = new BrokerService();
broker.setPersistent(false);
broker.setUseJmx(true);
broker.addConnector(brokerUrl);
return broker;
}
}
Configure JMS Producer
Let’s use Spring’s JmsTemplate to send messages to the embedded broker. We shall also use a pooled connection factory for our JmsTemplate as shown below.
Update JmsConfig.java
with JmsTemplate
bean.
com.codeaches.activmq.embedded.JmsConfig.java
@Bean
public JmsTemplate jmsTemplate() {
return new JmsTemplate(new PooledConnectionFactory(brokerUrl));
}
Create a producer class which uses the above created JmsTemplate
to send the data to embedded Active MQ server.
com.codeaches.activmq.embedded.JmsProducer.java
@Service
public class JmsProducer {
Logger log = LoggerFactory.getLogger(JmsProducer.class);
@Autowired
private JmsTemplate jmsTemplate;
@Value("${activemq.queue.name}")
String destination;
public void send(String message) {
jmsTemplate.convertAndSend(destination, message);
log.info("Sent message='{}'", message);
}
}
Configure JMS Consumer
Let’s use Spring’s JmsListener
to read the messages from the JMS. JmsListener
needs a connection factory. We shall use pooled connection factory in the consumer as well.
Update JmsConfig.java
with DefaultJmsListenerContainerFactory
bean.
com.codeaches.activmq.embedded.JmsConfig.java
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(new PooledConnectionFactory(brokerUrl));
return factory;
}
Create a consumer class which uses Spring’s JmsListener
to recieve the data from embedded Active MQ server.
com.codeaches.activmq.embedded.JmsConsumer.java
@Service
public class JmsConsumer {
Logger log = LoggerFactory.getLogger(JmsConsumer.class);
@JmsListener(destination = "${activemq.queue.name}")
public void receive(String message) {
log.info("Received message='{}'", message);
}
}
Now that we have both producer and consumer, let’s build a simple webservice method which uses earlier created JMS producer to send data to the queue my-queue-1
as shown below.
Here, the sendDataToJms(message)
webservice method forwards the message sent by the caller to JMS queue.
com.codeaches.activmq.embedded.MyRestController.java
@RestController
public class MyRestController {
@Autowired
JmsProducer jmsProducer;
@PostMapping("/send")
public void sendDataToJms(@RequestParam String message) {
jmsProducer.send(message);
}
}
Start the application
Tomcat console log:
JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
Using Persistence Adapter: MemoryPersistenceAdapter
Apache ActiveMQ 5.15.10 (localhost, ID:DESKTOP-MYPCNAME-53882-1325267078594-0:1) is starting
Listening for connections at: tcp://DESKTOP-MYPCNAME:61616
Connector tcp://DESKTOP-MYPCNAME:61616 started
Apache ActiveMQ 5.15.10 (localhost, ID:DESKTOP-MYPCNAME-53882-1325267078594-0:1) started
For help or more information please see: http://activemq.apache.org
Initializing ExecutorService 'applicationTaskExecutor'
Tomcat started on port(s): 8080 (http) with context path ''
Started EmbeddedJmsBrokerApplication in 2.17 seconds (JVM running for 3.17)
Test JMS producer and consumer
Let’s invoke the webservice by sending a hello message. JmsProducer
would send the message and JmsConsumer
will consume the hello message.
curl -i -X POST http://localhost:8080/send?message=hello
Tomcat Log
INFO com.codeaches.activmq.embedded.JmsProducer : Sent message='hello'
INFO com.codeaches.activmq.embedded.JmsConsumer : Received message='hello'
Summary
This concludes our guide to integrating an embedded Apache ActiveMQ JMS with a simple Spring Boot application.
Your feedback is always appreciated. Happy coding!
Related Posts: