1
在RabbitMQ的NACK消息,我有以下Consumer
类监听队列上的传入消息,然后两个ACK和NACK他们。 ack
部分工作正常,但nack
不起作用。由于某种原因,所有的消息都会得到回应。无法使用SprintBoot
application.properties中
spring.rabbitmq.host=192.168.99.100
spring.rabbitmq.port=5677
spring.rabbitmq.username=abc
spring.rabbitmq.password=def
spring.rabbitmq.listener.acknowledge-mode=manual
Producer.java
@Component
public class Producer implements CommandLineRunner {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private Queue queue;
@Override
public void run(String... args) throws Exception {
for (int i = 0; i < 100; i++) {
this.rabbitTemplate.convertAndSend(this.queue.getName(), "Hello World !");
}
}
}
Consumer.java
@Component
public class Consumer {
private final CountDownLatch latch = new CountDownLatch(1);
int ctr = 0;
@RabbitListener(queues = "producer-consumer-nack2.q")
public void receiveQueue(String text, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException, InterruptedException {
ctr++;
//nack every 10th, 20th, 30th and so on message
if (ctr % 10 == 0) {
System.out.println("Nack Message #" + ctr + ": " + text);
channel.basicNack(tag, false, true);
} else {
System.out.println("Ack Message #" + ctr + ": " + text);
channel.basicAck(tag, true);
}
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}