使用RabbitMQ实现延迟队列的两种方式
Linux安装RabbitMQ
首先创建用于连接RabbitMQ的Connection
package com.dfyang.rabbitmq;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
public class RabbitConnectionFactory {
private static final String IP_ADDRESS = "192.168.195.123";
private static final int PORT = 5672;
private static final String USERNAME = "root";
private static final String PASSWORD = "151310";
private static ConnectionFactory factory = new ConnectionFactory();
static {
factory.setHost(IP_ADDRESS);
factory.setPort(PORT);
factory.setUsername(USERNAME);
factory.setPassword(PASSWORD);
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = factory.newConnection();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
package com.dfyang.rabbitmq.dead;
import com.dfyang.rabbitmq.RabbitConnectionFactory;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.util.HashMap;
import java.util.Map;
public class TimeoutDLX {
public static void main(String[] args) throws Exception {
Connection connection = RabbitConnectionFactory.getConnection();
Channel channel = connection.createChannel();
//创建DLX及死信队列
channel.exchangeDeclare("dlx.exchange", "direct");
channel.queueDeclare("dlx.queue", true, false, false, null);
channel.queueBind("dlx.queue", "dlx.exchange", "dlx.routingKey");
//创建测试超时的Exchange及Queue
channel.exchangeDeclare("delay.exchange", "direct");
Map<String, Object> arguments = new HashMap<>();
//过期时间10s
arguments.put("x-message-ttl", 10000);
//绑定DLX
arguments.put("x-dead-letter-exchange", "dlx.exchange");
//绑定发送到DLX的RoutingKey
arguments.put("x-dead-letter-routing-key", "dlx.routingKey");
channel.queueDeclare("delay.queue", true, false, false, arguments);
channel.queueBind("delay.queue", "delay.exchange", "delay.routingKey");
//发布一条消息
channel.basicPublish("delay.exchange", "delay.routingKey", null, "该消息将在10s后发送到延迟队列".getBytes());
channel.close();
connection.close();
}
}
https://www.rabbitmq.com/community-plugins.html
注意:支持RabbitMQ 3.5.8及更高版本
启动插件:rabbitmq-plugins enable rabbitmq_delayed_message_exchange
——(关闭插件:rabbitmq-plugins disable rabbitmq_delayed_message_exchange)
注意x-delayed-type参数必须有,不然会报错。
package com.dfyang.rabbitmq.dead;
import com.dfyang.rabbitmq.RabbitConnectionFactory;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.util.HashMap;
import java.util.Map;
public class DelayExchangeDemo {
public static void main(String[] args) throws Exception {
Connection connection = RabbitConnectionFactory.getConnection();
Channel channel = connection.createChannel();
Map<String, Object> arguments = new HashMap<>();
arguments.put("x-delayed-type", "direct");
channel.exchangeDeclare("delay.plugin.exchange", "x-delayed-message", true, false, arguments);
channel.queueDeclare("delay.plugin.queue", true, false, false, null);
channel.queueBind("delay.plugin.queue", "delay.plugin.exchange", "delay.plugin.routingKey");
Map<String, Object> headers = new HashMap<>();
//延迟10s后发送
headers.put("x-delay", 10000);
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
builder.headers(headers);
channel.basicPublish("delay.plugin.exchange", "delay.plugin.routingKey", builder.build(), "该消息将在10s后发送到队列".getBytes());
channel.close();
connection.close();
}
}