rabbitmq消息队列设置过期时间和过期消息处理
适用场景
- 电商秒杀抢购活动中处理用户下单和付款时间不一致,设置过期时间,过期则不允许付款
参考 https://blog.csdn.net/zhu_tianwei/article/details/53563311
代码块
生产者
public class Producer {
private static String queue_name = "message_ttl_queue";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.31.2");
factory.setUsername("guest");
factory.setPassword("guest");
factory.setPort(5672);
factory.setVirtualHost("/guest");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
HashMap<String, Object> arguments = new HashMap<String, Object>();
arguments.put("x-dead-letter-exchange", "amq.direct");
arguments.put("x-dead-letter-routing-key", "message_ttl_routingKey");
channel.queueDeclare("delay_queue", true, false, false, arguments);
// 声明队列
channel.queueDeclare(queue_name, true, false, false, null);
// 绑定路由
channel.queueBind(queue_name, "amq.direct", "message_ttl_routingKey");
String message = "hello world!" + System.currentTimeMillis();
// 设置延时属性
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
// 持久性 non-persistent (1) or persistent (2)
AMQP.BasicProperties properties = builder.expiration("5000").deliveryMode(2).build();
// routingKey =delay_queue 进行转发
channel.basicPublish("", "delay_queue", properties, message.getBytes());
System.out.println("sent message: " + message + ",date:" + System.currentTimeMillis());
// 关闭频道和连接
channel.close();
connection.close();
}
}
消费者
public class Receiver {
private static String queue_name = "message_ttl_queue";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.31.2");
factory.setUsername("guest");
factory.setPassword("guest");
factory.setPort(5672);
factory.setVirtualHost("/guest");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(queue_name, true, false, false, null);
// 绑定路由
channel.queueBind(queue_name, "amq.direct", "message_ttl_routingKey");
// channel.exchangeDeclare("exchange_name", "direct", true);
// channel.queueBind("queue_name", "exchange_name", "");
channel.basicConsume(queue_name, false,
new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException {
String routingKey = envelope.getRoutingKey();
String contentType = properties.getContentType();
String str = new String(body);
System.out.println(str + " " + System.currentTimeMillis());
long deliveryTag = envelope.getDeliveryTag();
// (process the message components here ...)
channel.basicAck(deliveryTag, false);
}
});
System.out.println(channel);
}
}
自动回复与手动回复
channel.basicConsume(queue_name, false, ...)中flase表示手动回复,此时要手动回复,不然消息会持久存在队列中,回复代码如下
long deliveryTag = envelope.getDeliveryTag();
// (process the message components here ...)
channel.basicAck(deliveryTag, false);