RabbitMQ expand

RabbitMQ expand

Queue TTL / message

  1. Time To Live TTL is the abbreviation, that is, the survival time
  2. RabbitMQ supports message expiration time, when the message is sent can be specified
  3. RabbitMQ supports the expiration time of the queue, the message from the queue counted, as long as more than the timeout queue configuration, the message will automatically clear

Producers

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.victoria.rabbitmq.util.ConnectionUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class send {
    private static final String EXCHANGE_NAME = "test_exchange_ttl";
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取链接
        Connection connection = ConnectionUtils.getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //交换机声明
        channel.exchangeDeclare(EXCHANGE_NAME,"topic",true);
        //路由键
        String rount_key = "user.add";
        //发送消息
        for(int i = 0 ;i<5;i++){
            String msg_str = "Hello TTl msg"+i;
            channel.basicPublish(EXCHANGE_NAME,rount_key,null,msg_str.getBytes());
        }
        //关闭链接
        channel.close();
        connection.close();
    }
}

consumer

import com.rabbitmq.client.*;
import com.victoria.rabbitmq.util.ConnectionUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class recv {
    private static final String EXCHANGE_NAME = "test_exchange_ttl";
    private static final String QUEUE_NAME = "test_queue_ttl";
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取链接
        Connection connection = ConnectionUtils.getConnection();
        //创建通道
        final Channel channel = connection.createChannel();
        //限流
        channel.basicQos(1);
        //配置参数
        Map<String,Object> map = new HashMap<String, Object>();
        //配置ttl过期时间
        map.put("x-message-ttl", 10000);
        //声明队列和交换机
        channel.queueDeclare(QUEUE_NAME,true,false,false,map);
        channel.exchangeDeclare(EXCHANGE_NAME,"topic",true);
        //路由键匹配
        String rount_key = "user.#";
        //队列绑定交换机
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,rount_key);
        //消费者
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
                //ack认证
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        //监听
        channel.basicConsume(QUEUE_NAME,false,consumer);
    }
}

DLX (dead letter queue)

  1. Dead-Letter-Exchange
  2. DLX use, when the message becomes a dead letter (dead message) in a queue, it can be re-publish Exchange to another, this is DLX Exchange
  3. DLX is a normal Exchange, the Exchange and the general is no different, it can be specified on any queue, the queue is actually a set of attributes for the Dead Letter Queue
  4. When this has dead letter queue, the message will automatically RabbitMQ republish Exchange set up, and then be routed to another queue
  5. Can listen to the messages in the queue to do the appropriate treatment, this feature can make immediate parameter of function previously supported RabbitMQ3.0

Message into several cases the dead letter queue

  1. Message is rejected (basic.reject / basic.nack) and return queue arranged false requeue
  2. TTL expired messages
  3. The maximum length of the queue

Configuring the dead-letter queue

  1. You must first set the exchange dead letter queue and queue, then bind (switch name and the name of the dead letter queue queue can only write)
    1.Exchange: dlx.exchange
    2.Queue: dlx.queue
    3.RoutingKey: #
  2. Then normal switch statement, queue, binding, but need to add a queue can be extended parameters: arguments.put ( "x-dead-letter-exchange", "dlx.exchange");
  3. Thus the expiration message, reject or nack (requeue be arranged to false), the maximum queue length is reached, the message can be routed directly to the dead letter queue

Producers

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.victoria.rabbitmq.util.ConnectionUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class send {
    private static final String EXCHANGE_NAME = "test_exchange_dlx";
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取链接
        Connection connection = ConnectionUtils.getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //路由键
        String rount_key = "user.add";
        //消息十秒没被消费则过期
        AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().expiration("10000").build();
        //生产者发送消息
        for(int i =0;i<5;i++){
            String str_msg = "hello dlx msg"+i;
            channel.basicPublish(EXCHANGE_NAME,rount_key,properties,str_msg.getBytes());
        }
        //关闭链接
        channel.close();
        connection.close();
    }
}

consumer

import com.rabbitmq.client.*;
import com.victoria.rabbitmq.util.ConnectionUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class recv {
    private static final String EXCHANGE_NAME = "test_exchange_dlx";
    private static final String QUEUE_NAME = "test_queue_dlx";
    //注意:死信队列的交换机名和队列名只能这样取
    private static final String EXCHANGE_DLX_NAME = "dlx.exchange";
    private static final String QUEUE_DLX_NAME = "dlx.queue";
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取链接
        Connection connection = ConnectionUtils.getConnection();
        //创建通道
        final Channel channel = connection.createChannel();
        //限流
        channel.basicQos(1);
        //定义死信队列的队列和交换机
        channel.exchangeDeclare(EXCHANGE_DLX_NAME,"topic",true);
        channel.queueDeclare(QUEUE_DLX_NAME,true,false,false,null);
        //绑定
        channel.queueBind(QUEUE_DLX_NAME,EXCHANGE_DLX_NAME,"#");
        //定义交换机和队列
        channel.exchangeDeclare(EXCHANGE_NAME,"topic",true);
        //给与死信队列配置
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("x-dead-letter-exchange",EXCHANGE_DLX_NAME);
        channel.queueDeclare(QUEUE_NAME,true,false,false,map);
        //路由键
        String rount_key = "user.#";
        //绑定
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,rount_key);
        //消费者消费消息
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        //监听
        channel.basicConsume(QUEUE_NAME,false,consumer);
    }
}
发布了8 篇原创文章 · 获赞 0 · 访问量 230

Guess you like

Origin blog.csdn.net/weixin_44048746/article/details/104280164