Spring Boot ActiveMQ Support
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
Short Note on ActiveMQ
- 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
Create a Spring Boot Application
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>Packaging Structure
UserReceiver and ConfirmationReceiver
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
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
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
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
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("rameshfadatare@gmail.com", 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;
}
}
Demo
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner

Comments
Post a Comment
Leave Comment