Spring Boot JMS ActiveMQ Messaging Example - Java Guides
Maybe your like
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Bestseller 85% OFF
Building Real-Time REST APIs with Spring Boot - Blog App
Bestseller 85% OFF
Building Microservices with Spring Boot and Spring Cloud
Top Rated 85% OFF
Full-Stack Java Development with Spring Boot 4 and React
Bestseller 85% OFF
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Projects 85% OFF
Testing Spring Boot Application with JUnit and Mockito
Bestseller 85% OFF
ChatGPT for Java Developers: Boost Your Productivity with AI
Trending 85% OFF
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Bestseller 85% OFF
Master Spring Data JPA with Hibernate
Favorite 85% OFF
Spring Boot + Apache Kafka Course - The Practical Guide
Bestseller 85% OFF
Java Testing: Mastering JUnit 5 Framework
New 85% OFF
Reactive Programming in Java: Spring WebFlux and Testing
New 85% OFF
Spring Boot + RabbitMQ Course - The Practical Guide
Bestseller 85% OFF
Free Courses on YouTube Channel
175K SubscribersSpring Boot JMS ActiveMQ Messaging Example
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will create a simple Spring Boot JMS application that uses Spring’s JmsTemplate to post a single message and subscribes to it with a @JmsListener annotated method of a managed bean.Spring Boot ActiveMQ Support
Spring boot automatically configures the ConnectionFactory class if it detects ActiveMQ on the classpath. In this case, it also makes use of an embedded broker if does not find any ActiveMQ custom configurations in application.properties. In this example, we will be using the default ActiveMQ configuration. Below maven dependency provides all the required dependencies to integrate JMS and ActiveMQ with spring boot. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> The below dependency provides embedded ActiveMQ in the spring boot application: <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency>Short Note on ActiveMQ
There are quite a few JMS implementations out there. Most of them are provided by Middle Oriented Middleware providers, for example, WebSphere MQ, Oracle EMS, JBoss AP, SwiftMQ, TIBCO EMS, SonicMQ, ActiveMQ, and WebLogic JMS. Some are open-source, and some are not. Apache ActiveMQ, which was chosen as a JMS provider for this example, has the following characteristics:- the most popular and powerful open-source messaging and Integration Patterns server
- accepts non-Java clients
- can be used standalone in production environments
- supports pluggable transport protocols such as in-VM, TCP, SSL, NIO, UDP, multicast, JGroups, and JXTA transports
- can be used as an in-memory JMS provider, using an embedded broker, to avoid the overhead of running separate processes when doing unit testing JMS5
- the ActiveMQ executable starts with a default configuration
- can also be used embedded in an application
- can be configured using ActiveMQ or Spring configuration (XML or Java Configuration)
- provides advanced messaging features such as message groups, virtual and composite destinations, and wildcards
- provides support for Enterprise Integration Patterns when used with Spring Integration or Apache Camel
What we'll Build
We will build a Spring Boot JMS application that sends User instances wrapped up in JMS Messages to the userQueue.A message listener is configured to process the message and send a confirmation message on the confirmationQueue. Another listener is defined that waiting for the confirmation and printing its contents. To simplify the application even more, there is no need for a producer class and a consumer class. There is only one process that publishes and consumes messages from the two queues. The below diagram shows the Spring Boot JMS application abstract schema:Create a Spring Boot Application
There are many ways to create a Spring Boot application. The simplest way is to use Spring Initializr at http://start.spring.io/, which is an online Spring Boot application generator.Maven Dependencies - pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.javaguides.springboot</groupId> <artifactId>springboot-jms</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.4</version> </parent> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> spring-boot-starter-activemq: It provides all the required dependencies to integrate JMS and ActiveMQ with spring boot.activemq-broker : This provides embedded ActiveMQ in the spring boot application. But since we will be configuring our ActiveMQ outside the application we have commented on it for time being.spring-boot-maven-plugin: It will collect all the jar files present in the classpath and create a single executable jar.Packaging Structure
Following is the packing structure of our Spring boot JMS application -UserReceiver and ConfirmationReceiver
UserReceiver and ConfirmationReceiver have the responsibility of receiving the user and confirmation messages. The classes have a more practical implementation, making use of the @JmsListener annotation.UserReceiver.java
package net.javaguides.springboot.jms; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import javax.jms.Message; import java.util.concurrent.atomic.AtomicInteger; /** * Created by Ramesh Fadatare */ @Component public class UserReceiver { private Logger logger = LoggerFactory.getLogger(UserReceiver.class); private static AtomicInteger id = new AtomicInteger(); @Autowired ConfirmationSender confirmationSender; @JmsListener(destination = "userQueue", containerFactory = "connectionFactory") public void receiveMessage(User receivedUser, Message message) { logger.info(" >> Original received message: " + message); logger.info(" >> Received user: " + receivedUser); confirmationSender.sendMessage(new Confirmation(id.incrementAndGet(), "User " + receivedUser.getEmail() + " received.")); } }ConfirmationReceiver.java
The ConfirmationSender class is just a bean with a JmsTemplate bean injected in it that is being used to send a confirmation object to the confirmationQueue. package net.javaguides.springboot.jms; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; /** * Created by Ramesh Fadatare */ @Component public class ConfirmationReceiver { private Logger logger = LoggerFactory.getLogger(ConfirmationReceiver.class); @JmsListener(destination = "confirmationQueue", containerFactory = "connectionFactory") public void receiveConfirmation(Confirmation confirmation) { logger.info(" >> Received confirmation: " + confirmation); } }Creating POJO - User and Confirmation
Let's create a simple User.java POJO class that sends over a message queue:User.java
package net.javaguides.springboot.jms; /** * Created by Ramesh Fadatare */ public class User { private String email; private Double rating; private boolean active; public User() {} public User(String email, Double rating, boolean active) { this.email = email; this.rating = rating; this.active = active; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Double getRating() { return rating; } public void setRating(Double rating) { this.rating = rating; } public boolean isActive() { return active; } public void setActive(boolean active) { this.active = active; } @Override public String toString() { return "User{" + "email='" + email + '\'' + ", rating=" + rating + ", active=" + active + '}'; } }Confirmation.java
Let's create Confirmation.java class to confirm whether is message is reviewed on not: package net.javaguides.springboot.jms; /** * Created by Ramesh Fadatare */ public class Confirmation { private int ackNumber; private String verificationComment; public Confirmation() {} public Confirmation(int ackNumber, String verificationComment) { this.ackNumber = ackNumber; this.verificationComment = verificationComment; } public int getAckNumber() { return ackNumber; } public void setAckNumber(int ackNumber) { this.ackNumber = ackNumber; } public String getVerificationComment() { return verificationComment; } public void setVerificationComment(String verificationComment) { this.verificationComment = verificationComment; } @Override public String toString() { return "Confirmation{" + "ackNumber='" + ackNumber + '\'' + ", verificationComment=" + verificationComment + '}'; } }Send and Receive JMS messages with Spring - Application.java
The Application class is the configuration and starter of the application. It is annotated with the all-powerful @SpringBootApplication, which will make sure to scan for beans in the current package and will inject all infrastructure beans based on the classpath settings. The other annotation on this class is @EnableJms, and as you can probably speculate, this annotation enables the bean postprocessor that will process the @JmsListener annotations and will create the message listener container "under the hood". There is also a converter bean defined as type MappingJackson2MessageConverter implementing the JMS MessageConverter interface that transforms the sent objects into text JSON format. package net.javaguides.springboot; import jakarta.jms.ConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MessageConverter; import org.springframework.jms.support.converter.MessageType; import net.javaguides.springboot.jms.User; /** * Created by Ramesh Fadatare */ @SpringBootApplication @EnableJms public class Application { private static Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); //Send an user System.out.println("Sending an user message."); jmsTemplate.convertAndSend("userQueue", new User("[email protected]", 5 d, true)); logger.info("Waiting for user and confirmation ..."); System.in.read(); context.close(); } @Bean // Serialize message content to json using TextMessage public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } @Bean public JmsListenerContainerFactory << ? > connectionFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); // This provides all boot's default to this factory, including the message converter configurer.configure(factory, connectionFactory); // You could still override some of Boot's default if necessary. return factory; } } The JmsTemplate makes it very easy to send messages to destinations, and in this example is automatically created by Spring Boot. The connectionFactory is also created by Spring Boot and is backed up by ActiveMQ running in embedded mode.Demo
Execute the above Application class’s main() method. The main method, aside from starting up the application, instantiates a JmsTemplate object that is used to send a user object.
Clearly, the message was successfully sent and received. That’s all for this quick example of Spring JMSTemplate with embedded ActiveMQ. Comments
Post a Comment
Leave Comment
Spring Boot 3 Paid Course Published for Free on my Java Guides YouTube Channel
Subscribe to my YouTube Channel (165K+ subscribers): Java Guides Channel
Top 10 My Udemy Courses with Huge Discount: Udemy Courses - Ramesh Fadatare
Close
Master Python Programming and Libraries: Python Full Course
[NEW] Python full course for beginners with essential libraries like NumPy, Pandas, and Matplotlib for data science.
🆕 High-Demand 80–90% OFF
Spring 6 and Spring Boot 3 for Beginners (Includes 6 Projects)
In this course, you will learn Spring Core, Spring Boot 3, REST API, Spring MVC, WebFlux, Spring Security, Spring Data JPA, Docker, Thymeleaf & Building Projects.
🔥 Bestseller 80–90% OFF Available in Udemy for Business
Full-Stack Java Development with Spring Boot 3 and React
In this course, you will build two full-stack web applications (Employee Management System and Todo Management App) using Spring Boot, Spring Security, Spring Data JPA, JWT, React JS, and MySQL database.
🔥 Bestseller 80–90% OFF Available in Udemy for BusinessSubscriber to my top YouTube Channel (130K+ Subscribers)
Building Microservices with Spring Boot and Spring Cloud
Learn to build microservices using Spring Boot, Spring Cloud, React, Kafka, RabbitMQ, Docker, and REST API (REST Web Services).
🌟 Top Rated 80–90% OFF Available in Udemy for BusinessPremium Member-only Articles
- Read Premium Article on Medium
- Write for Us
Functional Programming in Java (Includes Java Collections)
Learn Java Lambda Expressions, Functional Interfaces, Stream API, and Collections Framework. Write Clean & Concise Code
🎓 Student Favorite 80–90% OFF
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
This course teaches you to build 5+ mini Spring Boot projects using Java 17+, REST API, Spring Boot 3, Spring Security 6, Thymeleaf, React, and MySQL database.
🌟 Top Rated 80–90% OFFJava Release-wise New Features
Java 8 Features
Java 9 Features
Java 10 Features
Java 11 Features
Java 12 Features
Java 13 Features
Java 14 Features
Java 15 Features
Java 16 Features
Java 17 Features
Java 18 Features
Java 19 Features
Java 20 Features
Java 21 Features
Spring Boot + Apache Kafka Course - The Practical Guide
Learn to use Apache Kafka as a broker to exchange messages between Producer and Consumer in Spring boot applications. 🎓 Student Favorite 80–90% OFF Available in Udemy for Business
ChatGPT + Generative AI + Prompt Engineering for Beginners
Unlock the Power of AI – Learn How to Use ChatGPT, Understand Generative AI, and Master Prompt Writing
🆕 New 80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
In this course, you will learn how to write Unit tests and Integration tests for Spring Boot App using JUnit, Mockito, AssertJ, Hamcrest, JsonPath, & Testcontainers.
🔥 Bestseller 80–90% OFF Available in Udemy for Business
ChatGPT for Java Developers: Boost Your Productivity with AI
Use ChatGPT as a Java developer to code faster, debug smarter, save time, boost productivity, & master AI-powered coding.
🚀 Trending Now 80–90% OFFTop Udemy Course: Spring Boot Thymeleaf Real-Time Web Application Course
My Udemy Course - Spring Boot RabbitMQ Course - Event-Driven Microservices
My Udemy Course - Master Spring Data JPA with Hibernate
My Udemy Course - Spring Boot + Apache Kafka Course
New Udemy Course: Build 5 Spring Boot Projects with Java: Line-by-Line Coding
About Me
Hi, I am Ramesh Fadatare. I am VMWare Certified Professional for Spring and Spring Boot.
I am the founder and author of this blog website JavaGuides, a technical blog dedicated to the Java/Java EE technologies, Python, Microservices, and Full-Stack Java development.
All the 5K+ articles, guides, and tutorials have been written by me, so contact me if you have any questions/queries. Read more about me at About Me.
Top YouTube Channel (175K+ Subscribers): Check out my YouTube channel for free videos and courses - Java Guides YouTube Channel
My Udemy Courses - My Top and Bestseller Udemy Course
Connect with me on Twitter, Facebook, LinkedIn, GitHub,
Follow Me on Twitter
Follow @FadatareRameshFacebook Likes and Shares
Tag » Activemq Xml Message Example
-
Xml Configuration - ActiveMQ - The Apache Software Foundation!
-
REST Interface · ActiveMQ Artemis Documentation
-
How To Send XML File To ActiveMQ? - Java - Stack Overflow
-
Receive XML Messages From ActiveMQ, Convert As JSON And Store ...
-
Apache ActiveMQ Best Practices Tutorial
-
Chapter 7. Creating Java Applications With ActiveMQ
-
Apache ActiveMQ Parameters - IBM
-
Queues Vs Topics And Examples With Java, Spring Boot And Apache ...
-
Spring JMS And ActiveMQ Integration - Point-to-point Domain
-
[PDF] Spring Boot With Activemq Example
-
Working Examples Of Using Java Message Service (JMS) With ...
-
10.3. Native ActiveMQ JMS-to-JMS Bridge (Deprecated)
-
Spring JMS XML Configuration Example