// 可靠生产
// https://www.rabbitmq.com/confirms.html
public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
// 1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 2. 设置连接属性
factory.setHost("114.67.85.157");
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = null;
Channel channel = null;
try {
// 3. 从连接工厂获取连接
connection = factory.newConnection("生产者");
// 4. 从连接中创建通道
channel = connection.createChannel();
// 进入 confirm 模式,每次发送消息,rabbitmq 处理之后会返回一个对应的回执消息
AMQP.Confirm.SelectOk selectOk = channel.confirmSelect();
// 增加监听器
ArrayList<String> queues = new ArrayList<>();
channel.addConfirmListener(new ConfirmListener() {
@Override
public void handleAck(long deliveryTag, boolean multiple) throws IOException {
// deliveryTag 同一个 channel 中此条消息的编号。
// 业务...
System.out.println("受理成功 " + queues.get((int) deliveryTag) + " " + multiple);
}
@Override
public void handleNack(long deliveryTag, boolean multiple) throws IOException {
// 失败重发
// queues.get((int) deliveryTag)
System.out.println("受理失败 " + deliveryTag);
}
});
// 受理 fanout 类型的交换器
channel.exchangeDeclare("ps_test", "fanout");
for (int i = 0; i < 10; i ++) {
// 消息内容
String message = "Hello confirm " + i;
queues.add(message);
// 发送消息到 ps_test 交换器上
AMQP.BasicProperties basicProperties = new AMQP.BasicProperties();
channel.basicPublish("ps_test", "", basicProperties, message.getBytes());
System.out.println("消息 " + message + "已发送!");
}
// 等待 20 秒
Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (channel != null && channel.isOpen()) {
channel.close();
}
if (connection != null && connection.isOpen()) {
connection.close();
}
}
}
}
/**
* 消息确认机制
*/
public class Consumer {
private static Runnable receive = () -> {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("114.67.85.157");
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = null;
Channel channel = null;
final String clientName = Thread.currentThread().getName();
try {
connection = factory.newConnection("消费者-" + clientName);
// 死信队列:专门用来存储出错、出异常的数据
channel = connection.createChannel();
channel.exchangeDeclare("dlq_exchange", "fanout");
channel.queueDeclare("dlq_queue1", false, false, false, null);
channel.queueBind("dlq_queue1", "dlq_exchange", "");
channel = connection.createChannel();
channel.exchangeDeclare("ps_test", "fanout");
String queueName = "queue1";
// 队列中有死信产生时,消息会转发到交换器 dlq_exchange
Map<String, Object> args = new HashMap<>();
args.put("x-dead-letter-exchange", "dlq_exchange");
channel.queueDeclare(queueName, false, false, false, args);
channel.queueBind(queueName, "ps_test", "");
Channel finalChannel = channel;
channel.basicConsume(queueName, false, "消费者-手动回执",
new DefaultConsumer(finalChannel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try {
System.out.println("收到消息:" + new String(body));
// TODO 业务
long deliveryTag = envelope.getDeliveryTag();
// 模拟业务处理耗时
Thread.sleep(1000);
// 正常消费
finalChannel.basicAck(deliveryTag, false);
// 异常消费
// finalChannel.basicNack(deliveryTag, false, true);
} catch (InterruptedException e) {
// 异常消费, requeue参数 true 重发,false 不重发(丢弃或者移到 DLQ 死信队列)
// finalChannel.basicNack(envelope.getDeliveryTag(), false, false);
e.printStackTrace();
}
}
});
System.out.println(clientName + " 开始接收消息");
System.in.read();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if (connection != null && connection.isOpen()) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
public static void main(String[] args) {
new Thread(receive, "c1").start();
}
}