ToB企服应用市场:ToB评测及商务社交产业平台

标题: 怎么保证RabbitMq消息不丢失 [打印本页]

作者: 伤心客    时间: 2025-2-13 16:02
标题: 怎么保证RabbitMq消息不丢失
生产者

  1. import com.rabbitmq.client.*;
  2. public class PersistentProducer {
  3.     private final static String QUEUE_NAME = "persistent_queue";
  4.     public static void main(String[] argv) throws Exception {
  5.         ConnectionFactory factory = new ConnectionFactory();
  6.         factory.setHost("localhost");
  7.         try (Connection connection = factory.newConnection();
  8.              Channel channel = connection.createChannel()) {
  9.             // 声明一个持久化队列
  10.             channel.queueDeclare(QUEUE_NAME, true, false, false, null);
  11.             // 启用生产者确认
  12.             channel.confirmSelect();
  13.             String message = "Persistent message with producer confirm!";
  14.             channel.basicPublish("", QUEUE_NAME,
  15.                 MessageProperties.PERSISTENT_TEXT_PLAIN,
  16.                 message.getBytes());
  17.             // 检查消息是否成功发送
  18.             if (channel.waitForConfirms()) {
  19.                 System.out.println("Message sent successfully!");
  20.             } else {
  21.                 System.out.println("Message failed to send!");
  22.             }
  23.         }
  24.     }
  25. }
复制代码
互换机

  1. import com.rabbitmq.client.*;
  2. public class DirectExchangeProducer {
  3.     private final static String EXCHANGE_NAME = "direct_logs";
  4.     private final static String QUEUE_NAME = "persistent_queue";
  5.     public static void main(String[] argv) throws Exception {
  6.         ConnectionFactory factory = new ConnectionFactory();
  7.         factory.setHost("localhost");
  8.         try (Connection connection = factory.newConnection();
  9.              Channel channel = connection.createChannel()) {
  10.             // 声明交换机和队列
  11.             channel.exchangeDeclare(EXCHANGE_NAME, "direct");
  12.             channel.queueDeclare(QUEUE_NAME, true, false, false, null);
  13.             channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
  14.             String message = "Hello, Direct Exchange!";
  15.             channel.basicPublish(EXCHANGE_NAME, "info",
  16.                 MessageProperties.PERSISTENT_TEXT_PLAIN,
  17.                 message.getBytes());
  18.             System.out.println("Sent: " + message);
  19.         }
  20.     }
  21. }
  22. public class DeadLetterConsumer {
  23.     private final static String DLX_QUEUE = "dlx_queue";
  24.     public static void main(String[] argv) throws Exception {
  25.         ConnectionFactory factory = new ConnectionFactory();
  26.         factory.setHost("localhost");
  27.         try (Connection connection = factory.newConnection();
  28.              Channel channel = connection.createChannel()) {
  29.             // 声明死信队列
  30.             channel.queueDeclare(DLX_QUEUE, true, false, false, null);
  31.             // 创建消费者回调
  32.             DeliverCallback deliverCallback = (consumerTag, delivery) -> {
  33.                 String message = new String(delivery.getBody(), "UTF-8");
  34.                 System.out.println("Dead Letter Queue Received: " + message);
  35.             };
  36.             // 设置死信队列消费者
  37.             channel.basicConsume(DLX_QUEUE, true, deliverCallback, consumerTag -> {});
  38.         }
  39.     }
  40. }
复制代码
消耗者

  1. import com.rabbitmq.client.*;
  2. public class AckConsumer {
  3.     private final static String QUEUE_NAME = "persistent_queue";
  4.     public static void main(String[] argv) throws Exception {
  5.         ConnectionFactory factory = new ConnectionFactory();
  6.         factory.setHost("localhost");
  7.         try (Connection connection = factory.newConnection();
  8.              Channel channel = connection.createChannel()) {
  9.             // 声明一个持久化队列
  10.             channel.queueDeclare(QUEUE_NAME, true, false, false, null);
  11.             // 创建一个消费者回调
  12.             DeliverCallback deliverCallback = (consumerTag, delivery) -> {
  13.                 String message = new String(delivery.getBody(), "UTF-8");
  14.                 System.out.println("Received: " + message);
  15.                 try {
  16.                     // 模拟消息处理
  17.                     if (message.contains("error")) {
  18.                         throw new Exception("Error while processing message");
  19.                     }
  20.                     // 手动确认消息
  21.                     channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
  22.                     System.out.println("Message processed and acknowledged");
  23.                 } catch (Exception e) {
  24.                     // 如果消息处理失败,可以将消息重新放回队列
  25.                     System.out.println("Error processing message, requeueing: " + e.getMessage());
  26.                     channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
  27.                 }
  28.             };
  29.             // 设置手动确认
  30.             channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
  31.         }
  32.     }
  33. }
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4