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 Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Object-Based Messaging in Spring Boot 3 with RabbitMQ – Code Samples & Tips

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: 5s

4. 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

validationRabbitMQspring-bootidle-eventMessage Converter
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.