Rabbit延迟队列实战

帅比萌擦擦* 提交于 2020-01-25 03:31:38

一 代码

1 生产者延迟3秒发送消息到死信队列代码,此时queue.normal队列充当的是延迟队列

package com.rabbitmq.ttl;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import com.rabbitmq.util.ConnectionUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
* 死信队列(路由键为rk的消息过期后被放置到死信队列)
*/
public class DealQueue {
    private static final String IP_ADDRESS = "127.0.0.1";
    private static final int PORT = 5672;//RabbitMQ(AMQP) 服务端默认端口号为 5672
    public static void main(String[] args) throws IOException,
            TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP_ADDRESS);
        factory.setUsername("root");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("exchange.dlx", "direct", true, false, null);
        channel.exchangeDeclare("exchange.normal", "fanout", true, false, null);

        Map<String, Object> arg = new HashMap<String, Object>(3);
        arg.put("x-message-ttl", 3000);
        arg.put("x-dead-letter-exchange", "exchange.dlx");

        channel.queueDeclare("queue.normal", false, false, false, arg);
        channel.queueBind("queue.normal", "exchange.normal", "");

        channel.queueDeclare("queue.dlx", false, false, false, null);
        channel.queueBind("queue.dlx", "exchange.dlx", "rk");

        String message = "delay message !";

        channel.basicPublish("exchange.normal", "rk", false,
                MessageProperties.PERSISTENT_TEXT_PLAIN,
                message.getBytes());

        ConnectionUtils.close(channel, connection);
    }
}

2 消费者从死信队列取消息代码

package com.rabbitmq.delay;

import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

// 延时消费
public class DelayQueue {
    private static final String IP_ADDRESS = "127.0.0.1";
    private static final int PORT = 5672;//RabbitMQ(AMQP) 服务端默认端口号为 5672
    public static void main(String[] args) throws IOException,
            TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP_ADDRESS);
        factory.setUsername("root");
        factory.setPassword("123456");
        Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();
        channel.basicQos(64);
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body)
                    throws IOException {
                System.out.println(" recv message: " + new String(body));
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        channel.basicConsume("queue.dlx", consumer);
    }
}

二 测试

1 消费者控制台打印如下

recv message: delay message !

三 参考代码

https://github.com/cakin24/RabbitMQDemo/blob/master/src/main/java/com/rabbitmq/ttl/DealQueue.java

https://github.com/cakin24/RabbitMQDemo/tree/master/src/main/java/com/rabbitmq/delay

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!