Rabbit延迟队列实战

一 代码

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值