Free Lines Arrow
본문 바로가기
Message Queue/RabbitMQ

[RbbitMQ] Delay RabbitMQ Spring Consume

by skahn1215 2022. 10. 17.
728x90
반응형

Delay RabbitMQ Spring Consume

  • 앞서 발행한 메세지를 실제로 받아보자

 

java code

package com.example.rabbitmqconsumer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

@SpringBootApplication
public class RabbitmqConsumerApplication {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setPassword("guest");
        factory.setUsername("guest");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueBind("message","delayed.message","*");
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {

            // 처리가 제대로 안될때 ack 를 보내지 않는다.
            try {
                String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                Thread.sleep(1000000L);
                System.out.println(" [x] Received '" + message + "'");
            } catch (Exception e) {
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        };
        channel.basicConsume("message",true, deliverCallback, consumerTag -> { });

    }

}

 

 

 

 

전체 소스코드

https://github.com/rnrl1215/delay-rabbitmq

 

GitHub - rnrl1215/delay-rabbitmq: delay-rabbitmq 연습

delay-rabbitmq 연습. Contribute to rnrl1215/delay-rabbitmq development by creating an account on GitHub.

github.com

 

728x90
반응형

'Message Queue > RabbitMQ' 카테고리의 다른 글

[RabbitMQ] Work Queues  (0) 2022.10.03
[RbbitMQ] RabbitMQ 개념 기초  (0) 2022.09.24
[RbbitMQ] Delay RabbitMQ Spring Publish  (0) 2022.09.24
[RbbitMQ] Delay RabbitMQ 설치하기  (0) 2022.09.24

댓글