临时队列
自动删除队列
自动删除队列和普通队列在使用上没有什么区别,唯一的区别是,当消费者断开连接时,队列将会被删除。自动删除队列允许的消费者没有限制,也就是说当这个队列上最后一个消费者断开连接才会执行删除。
自动删除队列只需要在声明队列时,设置属性auto-delete标识为true即可。系统声明的随机队列,缺省就是自动删除的。
单消费者队列
普通队列允许的消费者没有限制,多个消费者绑定到多个队列时,RabbitMQ会采用轮询进行投递。如果需要消费者独占队列,在队列创建的时候,设定属性exclusive为true。
自动过期队列
指队列在超过一定时间没使用,会被从RabbitMQ中被删除。什么是没使用?
一定时间内没有Get操作发生
没有Consumer连接在队列上
特别的:就算一直有消息进入队列,也不算队列在被使用。
通过声明队列时,设定x-expires参数即可,单位毫秒。
永久队列
队列的持久性
持久化队列和非持久化队列的区别是,持久化队列会被保存在磁盘中,固定并持久的存储,当Rabbit服务重启后,该队列会保持原来的状态在RabbitMQ中被管理,而非持久化队列不会被保存在磁盘中,Rabbit服务重启后队列就会消失。
非持久化比持久化的优势就是,由于非持久化不需要保存在磁盘中,所以使用速度就比持久化队列快。即是非持久化的性能要高于持久化。而持久化的优点就是会一直存在,不会随服务的重启或服务器的宕机而消失。
在声明队列时,将属性durable设置为“false”,则该队列为非持久化队列,设置成“true”时,该队列就为持久化队列
队列级别消息过期
就是为每个队列设置消息的超时时间。只要给队列设置x-message-ttl 参数,就设定了该队列所有消息的存活时间,时间单位是毫秒。如果声明队列时指定了死信交换器,则过期消息会成为死信消息。
队列保留参数列表
参数名 | 目的 |
x-dead-letter-exchange | 死信交换器 |
x-dead-letter-routing-key | 死信消息的可选路由键 |
x-expires | 队列在指定毫秒数后被删除 |
x-ha-policy | 创建HA队列 |
x-ha-nodes | HA队列的分布节点 |
x-max-length | 队列的最大消息数 |
x-message-ttl | 毫秒为单位的消息过期时间,队列级别 |
x-max-priority | 最大优先值为255的队列优先排序功能 |
在发送消息时,我们还可以对消息的属性做更细微的控制,比如构建Request-Response模式
生产者:
public class ReplyToProducer {
public final static String EXCHANGE_NAME = "replyto";
public static void main(String[] args)
throws IOException, TimeoutException {
/* 创建连接,连接到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);
//响应QueueName ,消费者将会把要返回的信息发送到该Queue
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);
}
}
消费者
public class ReplyToConsumer {
public static void main(String[] argv)
throws IOException, TimeoutException {
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";/*表示只关注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);
}
}
消息存活时间
当队列消息的TTL 和消息TTL都被设置,时间短的TTL设置生效。如果将一个过期消息发送给RabbitMQ,该消息不会路由到任何队列,而是直接丢弃。
为消息设置TTL有一个问题:RabbitMQ只对处于队头的消息判断是否过期(即不会扫描队列),所以,很可能队列中已存在死消息,但是队列并不知情。这会影响队列统计数据的正确性,妨碍队列及时释放资源。
消息的持久化
默认情况下,队列和交换器在服务器重启后都会消失,消息当然也是。将队列和交换器的durable属性设为true,缺省为false,但是消息要持久化还不够,还需要将消息在发布前,将投递模式设置为2。消息要持久化,必须要有持久化的队列、交换器和投递模式都为2。
消息属性的设置方法,包括如何将消息的持久化
生产者
public class MsgAttrProducer {
public final static String EXCHANGE_NAME = "msg_durable";
public static void main(String[] args)
throws IOException, TimeoutException {
/* 创建连接,连接到RabbitMQ*/
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
Connection connection = connectionFactory.newConnection();
/*创建信道*/
Channel channel = connection.createChannel();
/*创建持久化交换器*/
channel.exchangeDeclare(EXCHANGE_NAME,"direct",true);
/*日志消息级别,作为路由键使用*/
String[] serverities = {"error","info","warning"};
for(int i=0;i<3;i++){
String severity = serverities[i%3];
String msg = "Hellol,RabbitMq"+(i+1);
/*发布持久化消息*/
channel.basicPublish(EXCHANGE_NAME,severity,
MessageProperties.PERSISTENT_TEXT_PLAIN,
msg.getBytes());
}
channel.close();
connection.close();
}
}
消费者
public class MsgAttrConsumer {
public static void main(String[] argv)
throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
// 打开连接和创建频道,与发送端一样
Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
/*创建持久化交换器*/
channel.exchangeDeclare(MsgAttrProducer.EXCHANGE_NAME,
"direct",true);
/*声明一个持久化队列*/
String queueName = "msgdurable";
channel.queueDeclare(queueName,true,false,
false,null);
/*绑定,将队列和交换器通过路由键进行绑定*/
String routekey = "error";/*表示只关注error级别的日志消息*/
channel.queueBind(queueName,MsgAttrProducer.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);
}
};
/*消费者正式开始在指定队列上消费消息*/
channel.basicConsume(queueName,true,consumer);
}
}