Master Object-Based Messaging in Spring Boot 3 with RabbitMQ – Code Samples & Tips
This article demonstrates how to send and receive messages as objects in Spring Boot 3 using RabbitMQ, covering custom message converters, object validation, idle‑event handling, and custom listener containers with complete code examples.
Spring Boot 3 practical case collection now includes over 90 up‑to‑date examples and promises permanent updates for subscribers.
1. Object‑based message sending and receiving
1.1 Custom message converter
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}1.2 Sending a message
@Resource
private RabbitTemplate rabbitTemplate;
Person person = new Person();
rabbitTemplate.convertAndSend("person.exchange", "person.a", person);This automatically converts the Person object to JSON.
1.3 Message listener
@RabbitListener(id = "msg-queue", queues = "msg-queue", ackMode = "AUTO")
public void listener(Person person) {
System.out.printf("%s - Received message: %s%n", Thread.currentThread().getName(), person.toString());
}2. Message validation
2.1 Add validation dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>2.2 Define the message object
public class Person {
@NotEmpty(message = "Name cannot be empty")
private String name;
}2.3 Configure a validated listener
@Component
@Validated
public class MessageListener {
@RabbitListener(id = "msg-queue", queues = "msg-queue", ackMode = "AUTO")
public void listener2(@Valid Person person) {
System.out.printf("%s - Received message: %s%n", Thread.currentThread().getName(), person.toString());
}
}3. Listening for queue idle state
From Spring AMQP 1.6 you can listen for ListenerContainerIdleEvent when no messages are received for a configured period.
@Component
public class RabbitIdleApplicationListener implements ApplicationListener<ListenerContainerIdleEvent> {
@Override
public void onApplicationEvent(ListenerContainerIdleEvent event) {
System.out.printf("%s - No messages processed, idle time: %s%n",
Arrays.toString(event.getQueueNames()),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(event.getIdleTime())));
}
}Configure the interval in application.yml:
spring:
rabbitmq:
listener:
simple:
idle-event-interval: 5s4. Custom message listener container
@Bean
SimpleMessageListenerContainer factoryCreatedContainerNoListener(SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory) {
SimpleMessageListenerContainer container = rabbitListenerContainerFactory.createListenerContainer();
container.setListenerId("custom-listener");
container.setMessageListener(message -> {
System.out.printf("%s - Detected message: %s%n", Thread.currentThread().getName(), new String(message.getBody()));
});
container.setAcknowledgeMode(AcknowledgeMode.AUTO);
container.setQueueNames("test-1");
return container;
}You can define multiple containers as needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
