小Tips:
大家可以通过RabbitMQ的web插件查看本文中的大部分信息。
队列的声明的5个参数分别是:
1.队列名称
2.队列是否持久化
3.队列是否具有排他性(只有同一连接共享此队列,且连接断开时队列删除)排他队列详细说明
4.队列是否自动删除
自动删除是队列经历过至少一次连接后,所有的消费者都断开了连接,此时队列会进行自动删除。
5.封装队列的一些参数
Message TTL:
How long a message published to a queue can live before it is discarded. 一个消息被发布在队列里最多能存活的时间。
//通过设置队列的消息存活时间
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-message-ttl", 60000);
channel.queueDeclare(QUEUE_NAME, false, false, false, args);
//也可以通过发布消息时,给消息绑定时间
byte[] messageBodyBytes = "Hello, world!".getBytes();
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.expiration("60000")
.build();
channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);
如果俩者都设置了过期时间,那么会选择更小的那一个作为真正的消息存活时间。
Auto expire:
How long a queue can be unused for before it is automatically deleted (milliseconds).
(Sets the “x-expires” argument.)
一个队列的存活时间
Queues will expire after a period of time only when they are not used (e.g. do not have consumers) 没有消费者后,进行倒计时。
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-expires", 1800000);
channel.queueDeclare("myqueue", false, false, false, args);
Max length:
How many (ready) messages a queue can contain before it starts to drop them from its head.
(Sets the “x-max-length” argument.) 设置队列消息最大数。
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-length", 10); //队列最多容下10条消息
channel.queueDeclare("myqueue", false, false, false, args);
Max length bytes:
Total body size for ready messages a queue can contain before it starts to drop them from its head.
(Sets the “x-max-length-bytes” argument.) 设置单条消息最长字节数(只针对消息体)
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-length-bytes", 1024); //单条消息最长字节为1024字节
channel.queueDeclare("myqueue", false, false, false, args);
后面几个参数后面再补QAQ