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
<code>@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}</code>1.2 Sending a message
<code>@Resource
private RabbitTemplate rabbitTemplate;
Person person = new Person();
rabbitTemplate.convertAndSend("person.exchange", "person.a", person);
</code>This automatically converts the Person object to JSON.
1.3 Message listener
<code>@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());
}
</code>2. Message validation
2.1 Add validation dependency
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</code>2.2 Define the message object
<code>public class Person {
@NotEmpty(message = "Name cannot be empty")
private String name;
}
</code>2.3 Configure a validated listener
<code>@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());
}
}
</code>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.
<code>@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())));
}
}
</code>Configure the interval in application.yml :
<code>spring:
rabbitmq:
listener:
simple:
idle-event-interval: 5s
</code>4. Custom message listener container
<code>@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;
}
</code>You can define multiple containers as needed.
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.