rabbitmq 消费端ACK与重回队列机制

本文详细介绍了一种基于RabbitMQ的消息确认机制实现,通过Java代码示例展示了如何配置生产者和消费者,实现消息的发布、接收及确认过程,特别关注了消息持久化和手动确认的细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.HashMap;
import java.util.Map;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {
   public static void main(String[] args) throws Exception {
      ConnectionFactory connectionFactory = new ConnectionFactory();
      connectionFactory.setHost("192.168.6.1");
      connectionFactory.setPort(5672);
      connectionFactory.setVirtualHost("/");
      connectionFactory.setUsername("longlong");
      connectionFactory.setPassword("");
      Connection connection = connectionFactory.newConnection();
      Channel channel = connection.createChannel();
      
      String exchange = "test_ack_exchange";
      String routingKey = "ack.save";
      for(int i =0; i<5; i ++){
         Map<String, Object> headers = new HashMap<>();
         headers.put("num", i);
         
         AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
               .deliveryMode(2)
               .contentEncoding("UTF-8")
               .headers(headers)
               .build();
         String msg = "Hello RabbitMQ ACK Message " + i;
         channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes());
      }
      
   }
}
import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class MyConsumer extends DefaultConsumer {
   private Channel channel ;
   
   public MyConsumer(Channel channel) {
      super(channel);
      this.channel = channel;
   }

   @Override
   public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
      System.err.println("-----------consume message----------");
      System.err.println("body: " + new String(body));
      try {
         Thread.sleep(2000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
      if((Integer)properties.getHeaders().get("num") == 0) {
         channel.basicNack(envelope.getDeliveryTag(), false, true);
      } else {
         channel.basicAck(envelope.getDeliveryTag(), false);
      }
      
   }
}
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Consumer {
   public static void main(String[] args) throws Exception {
      ConnectionFactory connectionFactory = new ConnectionFactory();
      connectionFactory.setHost("192.168.6.1");
      connectionFactory.setPort(5672);
      connectionFactory.setVirtualHost("/");
      connectionFactory.setUsername("longlong");
      connectionFactory.setPassword("");
      
      Connection connection = connectionFactory.newConnection();
      Channel channel = connection.createChannel();

      String exchangeName = "test_ack_exchange";
      String queueName = "test_ack_queue";
      String routingKey = "ack.#";
      
      channel.exchangeDeclare(exchangeName, "topic", true, false, null);
      channel.queueDeclare(queueName, true, false, false, null);
      channel.queueBind(queueName, exchangeName, routingKey);
      
      // 手工签收 必须要关闭 autoAck = false
      channel.basicConsume(queueName, false, new MyConsumer(channel));
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值