五、RabbitMQ控制队列

1.临时队列

1.1 自动删除队列

自动删除队列和普通队列在使用上没有什么区别,唯一的区别是,当消费者断开连接时,队列将会被删除。自动删除队列允许的消费者没有限制,也就是说当这个队列上最后一个消费者断开连接才会执行删除。

自动删除队列只需要在声明队列时,设置属性auto-delete标识为true即可。系统声明的随机队列,缺省就是自动删除的。

在这里插入图片描述

1.2 单消费者队列

普通队列允许的消费者没有限制,多个消费者绑定到多个队列时,RabbitMQ会采用轮询进行投递。如果需要消费者独占队列,在队列创建的时候,设定属性exclusive为true。

在这里插入图片描述

1.3 自动过期队列

指队列在超过一定时间没使用,会被从RabbitMQ中被删除。什么是没使用?

  • 一定时间内没有Get操作发生
  • 没有Consumer连接在队列上
    特别的:就算一直有消息进入队列,也不算队列在被使用。
    通过声明队列时,设定x-expires参数即可,单位毫秒。

在这里插入图片描述

2.永久队列

持久化队列和非持久化队列的区别是,持久化队列会被保存在磁盘中,固定并持久的存储,当Rabbit服务重启后,该队列会保持原来的状态在RabbitMQ中被管理,而非持久化队列不会被保存在磁盘中,Rabbit服务重启后队列就会消失。

非持久化比持久化的优势就是,由于非持久化不需要保存在磁盘中,所以使用速度就比持久化队列快。即是非持久化的性能要高于持久化。而持久化的优点就是会一直存在,不会随服务的重启或服务器的宕机而消失。

在声明队列时,将属性durable设置为“false”,则该队列为非持久化队列,设置成“true”时,该队列就为持久化队列

在这里插入图片描述

3.队列级别消息过期

就是为每个队列设置消息的超时时间。只要给队列设置x-message-ttl 参数,就设定了该队列所有消息的存活时间,时间单位是毫秒。如果声明队列时指定了死信交换器,则过期消息会成为死信消息。

在这里插入图片描述

4.队列保留参数列表

在这里插入图片描述

5.消息的属性

在这里插入图片描述

在发送消息时,我们还可以对消息的属性做更细微的控制,比如构建Request-Response模式

  • ReplyToProducer
package setmsg;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;

/**
 * @Author: Rab
 * @Date: 2020-04-16 15:09
 * @Description:
 */
public class ReplyToProducer {

    public final static String EXCHANGE_NAME = "replyto";

    public static void main(String[] args) throws Exception {

        /* 创建连接,连接到RabbitMQ*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        Connection connection = connectionFactory.newConnection();

        /*创建信道*/
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct", false);

        //响应消费者回传的消息
        String responseQueue = channel.queueDeclare().getQueue();

        //消息的唯一id
        String msgId = UUID.randomUUID().toString();
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().replyTo(responseQueue).messageId(msgId).build();

        /*声明了一个消费者*/
        final Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("Received[" + envelope.getRoutingKey()
                        + "]" + message);
            }
        };
        /*消费者正式开始在指定队列上消费消息*/
        channel.basicConsume(responseQueue, true, consumer);

        String msg = "Hellol,RabbitMq";

        channel.basicPublish(EXCHANGE_NAME, "error", properties, msg.getBytes());
        System.out.println("Sent error:" + msg);
    }
}
  • ReplyToConsumer
package setmsg;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author: Rab
 * @Date: 2020-04-16 15:16
 * @Description:
 */
public class ReplyToConsumer {

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");

        // 打开连接和创建频道,与发送端一样
        Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();

        //创建交换器
        channel.exchangeDeclare(ReplyToProducer.EXCHANGE_NAME, "direct", false);

        String queueName = "replyto";

        channel.queueDeclare(queueName, false, false, false, null);

        String routekey = "error";

        channel.queueBind(queueName, ReplyToProducer.EXCHANGE_NAME, routekey);

        System.out.println("waiting for message........");

        /*声明了一个消费者*/
        final Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("Received[" + envelope.getRoutingKey()
                        + "]" + message);

                AMQP.BasicProperties respProp = new AMQP.BasicProperties().builder().replyTo(properties.getReplyTo()).correlationId(properties.getMessageId()).build();
                channel.basicPublish("", respProp.getReplyTo(), respProp, ("Hi," + message).getBytes("UTF-8"));
            }
        };
        /*消费者正式开始在指定队列上消费消息*/
        channel.basicConsume(queueName, true, consumer);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值