RabbitMQ:Queue的介绍和使用

1.声明

当前内容用于本人学习和使用当前的Queue,当前内容为RabbitMQ中对Queue的介绍

当前内容来源:RabbitMQ中的Queue

2.Queue的官方介绍

首先先分析以下前面的Queue的使用,其实这个东西就是一个队列,一个存储消息的队列,并且可以持久化,而且这个队列中的消息可以被消费,如果出现死信则是死信队列,死信队列也是可以被处理的

接下来就是官方介绍

Queues in the AMQP 0-9-1 model are very similar to queues in other message- and task-queueing systems: they store messages that are consumed by applications. Queues share some properties with exchanges, but also have some additional properties:

这里的意思是:Queue在这个AMQP 0-9-1模型中和消息队列和任务队列系统是非常类似的:他们都是存储消息然后再程序中被消费。队列可以共享一些属性到交换机中,单同样有一些其他的属性

官方属性

  1. Name
  2. Durable (the queue will survive a broker restart)
  3. Exclusive (used by only one connection and the queue will be deleted when that connection closes)
  4. Auto-delete (queue that has had at least one consumer is deleted when last consumer unsubscribes)
  5. Arguments (optional; used by plugins and broker-specific features such as message TTL, queue length limit, etc)

翻译过来就是

  1. 当前的queue具有名称name属性
  2. Excusive属性(只能用在一个连接上面,如果这个唯一的连接都关闭那么这个队列将被删除)
  3. 自动删除属性(队列至少有一个消费者,如果这个最后一个消费者都没了,那么这个队列将被删除)
  4. 参数(由插件和特定于代理的功能使用,例如消息TTL,队列长度限制等)

我们发现了这个可以通过为该队列添加参数从而限制当前队列的长度

接下来的因该就是使用建议

Before a queue can be used it has to be declared. Declaring a queue will cause it to be created if it does not already exist. The declaration will have no effect if the queue does already exist and its attributes are the same as those in the declaration. When the existing queue attributes are not the same as those in the declaration a channel-level exception with code 406 (PRECONDITION_FAILED) will be raised.

应该先声明一个队列然后再使用。如果声明一个队列不存在那么它将被常见。如果这个队列再声明的时候已经存在了,并且属性都一直不会有问题。如果这个声明的时候的属性和已经存在的队列的属性存在冲突或者不一致,那么将出现channel-level 异常,并且为406代码

3.测试声明queue的使用建议

1.创建406错误,制造属性冲突

public class QueueCreateTest {
	public static void main(String[] args) throws Exception {
		RabbitMqUtils mqUtils = new RabbitMqUtils();
		mqUtils.reciver("hello", true,(x, y) -> {
			System.out.println(new String(y.getBody(), "utf-8"));
		});
	}
}

启动结果
在这里插入图片描述
直接典型的406错误,是哪里不一致呢?,这个是要看我们创建hello的时候使用的属性

使用当前rabbitMQ的api方式查看所有queue:http://localhost:15672/api/queues

最后找到hello的属性,下面是提取后的数据

        "auto_delete":false,
        "consumer_utilisation":null,
        "consumers":0,
        "durable":false,       
        "exclusive":false,
        "idle_since":"2020-05-13 10:45:16",
        "messages":0,
        "messages_details":{
            "rate":0
        },
        "messages_unacknowledged_ram":0,
        "name":"hello",
        "node":"rabbit@LAPTOP-KLPPTN3T",
        "single_active_consumer_tag":null,
        "state":"running",
        "type":"classic",
        "vhost":"/"

对比发现durable属性不一致,所以导致的错误

修改这个durable就是第二个参数为false即可
在这里插入图片描述

4.官方的Queue Name

Queue Names
Applications may pick queue names or ask the broker to generate a name for them. Queue names may be up to 255 bytes of UTF-8 characters. An AMQP 0-9-1 broker can generate a unique queue name on behalf of an app. To use this feature, pass an empty string as the queue name argument. The generated name will be returned to the client with queue declaration response.

Queue names starting with “amq.” are reserved for internal use by the broker. Attempts to declare a queue with a name that violates this rule will result in a channel-level exception with reply code 403 (ACCESS_REFUSED).

翻译后的结果
应用程序可以选择队列名称,也可以要求代理为它们生成名称,队列名称应该再255个utf-8字符.一个AMQP 0-9-1代理可以在一个app中生成一个唯一的队列名称。使用这个代理方式生成应该传递一个空的队列名称,然后这个创建的名称将会以响应的方式返回

如果使用amp.开头的队列是当前内部系统所使用的,如果声明一个这样开头的队列名称将会触发一个异常,并返回403

通过这个简介,发现可以直接传递一个没有名字的队列名称过去让当前的代理自动创建一个唯一的队列名称并返回,我们不能使用amp.开头的,这是系统内部使用的,如果尝试创建以amp.开头则会出现异常

下面是测试使用AMQP 0-9-1代理生成一个唯一的队列

public class QueueNameCreateTest {
	public static void main(String[] args) throws Exception {
		RabbitMqUtils mqUtils = new RabbitMqUtils();
		mqUtils.reciver("", false,(x, y) -> {
			System.out.println(new String(y.getBody(), "utf-8"));
		});
	}
}

直接声明一个“”字符的队列,然后查看结果
在这里插入图片描述
发现创建了一个amp.genXXX之类的一个唯一的queue名称

控制台结果
在这里插入图片描述
测试成功

5.官方的Queue Durability

Queue Durability
Durable queues are persisted to disk and thus survive broker restarts. Queues that are not durable are called transient. Not all scenarios and use cases mandate queues to be durable.

Durability of a queue does not make messages that are routed to that queue durable. If broker is taken down and then brought back up, durable queue will be re-declared during broker startup, however, only persistent messages will be recovered.

翻译过来就是

队列持久化

持久化可以保存到硬盘上面,可以重启后恢复。如果不启用持久化默认就是瞬时的,并非所有的队列都需要被持久化

队列持久化不会让消息从路由到队列持久化(就是消息从路由到队列的时候并不是持久化的),如果当前的代理关闭,那么将会被备份,代理重启之后持久化队列就会将被加载,但是只有消息持久化被恢复

分析发现:消息持久化、队列持久化、exchange持久化

  1. 在如果想实现消息宕机恢复,就必然开启队列持久化和exchange持久化
  2. 但是消息持久化才是消息被恢复的(如果开启消息持久化)

6.总结

1.从当前的RabbitMQ的Queue的介绍发现,默认声明一个队列的时候可以不提供名称指定为""空字符串,则会让AMQP代理自动创建一个唯一的Queue的名称

2.如果声明的Queue已经存在了,那么就会出现参数校验,如果参数不同则报错

3.Queue可以被持久化到磁盘,宕机后可以恢复Queue,但是消息必须是持久化的,如果消息不是持久化那么可能出现消息丢失的情况

以上纯属个人见解,如有问题请联系本人!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值