Spring-Cloud

RabbitMQ - 3: 메시지 수신자

  • -

이번 포스트에서는 Spring Cloud에서 RabbitMQ 메시지를 수신하는 수신자를 구성해보자.

 

프로젝트 구성

 

의존성

의존성은 기존에 발신자와 동일하게 구성한다.

더보기

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>4.0.3</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>com.quietjun</groupId>

<artifactId>rabbit</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>rabbitMQ_redeiver</name>

<description>Demo project for Spring Boot</description>

<url/>

<licenses>

<license/>

</licenses>

<developers>

<developer/>

</developers>

<scm>

<connection/>

<developerConnection/>

<tag/>

<url/>

</scm>

<properties>

<java.version>21</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-amqp</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-webmvc</artifactId>

</dependency>

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-amqp-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-webmvc-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

 

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<annotationProcessorPaths>

<path>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</path>

</annotationProcessorPaths>

</configuration>

</plugin>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<excludes>

<exclude>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</exclude>

</excludes>

</configuration>

</plugin>

</plugins>

</build>

 

</project>

 

application.properties

여기서도 특별한 처리는 필요 없다. 단지 서버의 port를 9099로 지정해주자.

 

기본 코드

 

환경 설정

sender와 마찬가지로 MessageConverter만 등록해주면 된다.

@Configuration
@EnableRabbit
public class GenericRabbitConfig {

    //@Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory,  MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }

    @Bean
    public MessageConverter jacksonJsonMessageConverter(JsonMapper jsonMapper) {
        return new JacksonJsonMessageConverter(jsonMapper);
    }
}

 

@RabbitListener

메시지 수신자의 핵심은 @RabbitListener를 이용한 수신자 구성이다.

@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@MessageMapping
@Repeatable(RabbitListeners.class)
public @interface RabbitListener {
	String[] queues() default {}; // listen 할 대상 큐 지정
	. . .
}

@RabbitListener는 Type 또는 Method에 선언하는 애너테이션으로 주요 속성은 listen할 대상을 지정하는 queues 속성이다.

그리고 메시지를 listen하는 과정에서는 direct, topic이라도 별도의 key를 설정할 필요는 없다. 

@Component
@Slf4j
public class MessageListener {
    @RabbitListener(queues = "directQueue")
    public void getDirectMessage(MessageVO mvo) {
        log.debug("from direct Queue ===> {}" , mvo);
    }
    @RabbitListener(queues = "fanoutQueue")
    public void getFanoutMessage(MessageVO mvo) {
        log.debug("from fanout Queue ===> {}" , mvo);
    }
    @RabbitListener(queues = "topicQueue")
    public void getTopicMessage(MessageVO mvo) {
        log.debug("from Topic Queue ===> {}" , mvo);
    }

    public record MessageVO(String tag, String msg, Integer number) {}
}

 

동작 확인

이제 sender가 메시지를 보내면 receiver의 각 메서드가 잘 동작하는 것을 확인할 수 있다.

from direct Queue ===> MessageVO[tag=direct, msg=hong님이 RabbitMQ 브로커에게 메시지를 보내요, number=1]
from fanout Queue ===> MessageVO[tag=fanout, msg=hong님이 RabbitMQ 브로커에게 메시지를 보내요, number=2]
from Topic Queue ===> MessageVO[tag=topic, msg=hong님이 RabbitMQ 브로커에게 메시지를 보내요, number=3]

 

'Spring-Cloud' 카테고리의 다른 글

RabbitMQ - 2: 메시지 발신자  (0) 2026.02.02
RabbitMQ - 1:기본  (0) 2026.02.01
01. OpenFeign 1  (0) 2026.01.24
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.