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

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));
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ 中,消费者可以通过创建一个消息监听器来监听指定的队列,并在接收到消息后进行消息确认(ACK)。 下面是一个示例代码,展示了如何使用 RabbitMQ Java 客户库来实现消息监听和消息确认: ```java import com.rabbitmq.client.*; public class Consumer { private final static String QUEUE_NAME = "myQueue"; public static void main(String[] argv) throws Exception { // 创建连接工厂,并设置 RabbitMQ 服务器的连接信息 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setUsername("your_username"); factory.setPassword("your_password"); // 创建连接 Connection connection = factory.newConnection(); // 创建消息信道 Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 创建消费者并设置消息处理逻辑 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("Received message: " + message); // 处理消息逻辑 // 手动发送消息确认 channel.basicAck(envelope.getDeliveryTag(), false); } }; // 设置消息确认模式为手动确认 channel.basicConsume(QUEUE_NAME, false, consumer); // 等待消息 while (true) { Thread.sleep(1000); } } } ``` 在上述代码中,首先创建了一个连接工厂,并设置 RabbitMQ 服务器的连接信息。然后创建连接和消息信道,声明了要消费队列。 接着创建了一个继承自 DefaultConsumer 的消费者对象,并重写了 handleDelivery 方法来处理接收到的消息。在处理消息的逻辑之后,使用 `channel.basicAck(envelope.getDeliveryTag(), false)` 手动发送消息确认。 最后,通过调用 `channel.basicConsume(QUEUE_NAME, false, consumer)` 来设置消费者的消息确认模式为手动确认,并开始等待消息的到来。 请根据你的实际需求进行相应的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值