메시지 큐는 메시지 지향 미들웨어(Message Oriented Middleware, MOM)의 한 형태로, 애플리케이션 간 비동기적으로 데이터를 주고받는 시스템입니다.
우체통처럼 메시지를 보관하고 전달하는 중간 저장소 역할을 수행합니다.
생산자(Producer)가 메시지를 발행하면 중간에 큐에 저장되고, 소비자(Consumer)가 필요할 때 꺼내 처리합니다.
메시지 큐는 본질적으로 비동기입니다.
장점
고려사항
Producer
Consumer
RabbitMQ는 AMQP(Advanced Message Queuing Protocol) 기반의 오픈소스 메시지 브로커입니다. Erlang으로 개발되었으며, 엔터프라이즈급 메시징 솔루션으로 널리 사용됩니다.
안정성, 유연성, 사용 편의성이 높다고 알려져 있습니다.
장점
단점
RabbitMQ 아키텍처 구성요소
[Producer]
|
v
+-----------+
| Exchange |
+-----------+
/ | \
v v v
[Q1] [Q2] [Q3]
| | |
v v v
[Con1] [Con2] [Con3]
1) Producer(메시지 생성자)
2) Exchange(메시지 라우터/교환기)
3) Binding(연결 설정)
4) Queue(메시지 큐)
5) Consumer(소비자)
Exchange 타입별 특징



@Bean
public Queue durableQueue() {
return QueueBuilder.durable("my-queue").build();
}
application.yml
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
publisher-confirm-type: correlated # 또는 simple
publisher-returns: true
// Confirm Callback 등록
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
// 메시지 성공/실패 확인
template.setConfirmCallback((correlationData, ack, cause) -> {
if (ack) {
System.out.println("✅ 메시지 전송 성공");
} else {
System.err.println("❌ 메시지 전송 실패: " + cause);
}
});
// 메시지가 라우팅되지 못할 경우 처리
template.setReturnsCallback(returned -> {
System.err.println("❌ 라우팅 실패 메시지: " + new String(returned.getMessage().getBody()));
});
return template;
}
application.yml
spring:
rabbitmq:
listener:
simple:
acknowledge-mode: manual # 수동 ack 설정
@RabbitListener(queues = "my-queue")
public void receiveMessage(Message message, Channel channel) throws IOException {
try {
String msg = new String(message.getBody());
System.out.println("수신된 메시지: " + msg);
// 비즈니스 로직 처리...
// 성공 시 ack
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
// 실패 시 메시지를 다시 큐에 넣거나 폐기
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
}
}