Negative Acknowledgements (Nacks) and Dead Letter Queues (DLQ)
We will use project Nacks to explore negative acknowledgements and Dead Letter Queue.
Start SNProducerString to begin publishing messages to topic summitstudent1/developer/delayedmessages.
Start NackingConsumer to begin consuming messages. The following configuration has been added to the consumer:
.negativeAckRedeliveryDelay(2,TimeUnit.SECONDS)
.deadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(2).build())
Each time a messages contains a value of 1, it will be negatively acknowledged.
for (int i = 0; i < 10000; i++) {
Message<String> msg = consumer.receive();
System.out.println("Receive message " + msg.getValue());
if(msg.getValue().contains("1")) {
consumer.negativeAcknowledge(msg);
System.out.println(" nacking");
} else {
System.out.println(" acknowledging");
consumer.acknowledge(msg);
}
}
After being redelivered twice and still not acknowledged, the messages will be sent to the Dead Letter Queue (DLQ).
To view messages sent to the DLQ, execute DLQConsumer.

DLQ can be used with Shared and Key-Shared subscriptions.
