RabbitMQ 지연 큐(delayed queue)

delayed queue 큐는 말그대로 메시지 보낸 바로 즉시 받는게 아니라 나중에 받는다는 의미.

지연 큐 사용적합한 상황 아래와 같은 경우일것 같다.

  • 주문기능 : 일정한 시간 지나도 최종 결제를 하지 않으면 주문 자동취소 되게 하려는 경우.

  • 메시지 기능 : 주문완료후 60초 이내 고객에 메시지를 전송하도록 하고싶은 경우.

  • 실패에 대한 재시도 : 업무시도 실패 후 일정한 시간후 재시도 하게끔 하는 경우.

이외 에도 많은 곳에서 지연 큐를 사용할수 있을것으로 추정된다.

RabbitMQ 자체는 지연 기능이 없지만 Time-To-Live Extensions 과 Dead Letter Exchange 의 특징을 이용하여 지연큐를 기능을 구현해 낼수 있다.

pom.xml 에 spring-boot-starter-amqp dependency 추가

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies

application.properties 에 rabbitmq 설정을 추가 한다.

Book (demo) 클래스를 만든다.

controller 작성

메시지 소비자(Consumer)

메인 클래스

RabbitMqDemoApplication 를 구동하면 아래와 같은 메시지가 출력 될것이다.

Last updated