spring-attic / spring-cloud-gcp

Integration for Google Cloud Platform APIs with Spring
Apache License 2.0
704 stars 694 forks source link

Manually ack/nack with spring cloud pubsub streaming #2566

Closed thayhoang closed 3 years ago

thayhoang commented 3 years ago

How can I manually ack/nack message using spring could pubsub streaming? With rabbitMq, I can do something like this

@StreamListener(target = BAR)
public void handlerMethod(Foo foo, @Header(AmqpHeaders.CHANNEL) Channel channel,
                          @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) {
    if (....) {
       channel.basickAck(deliveryTag, false);
    } else {
       channel.basicNack(deliveryTag, false, true);
    }
}

Currently, with pubsub, I have to throw an Exception in order to nack the message. I'm wonder if there's a cleaner way.

elefeint commented 3 years ago

There is! 1) Set manual acking through spring.cloud.stream.gcp.pubsub.bindings.{CONSUMER_NAME}.consumer.ack-mode property (docs)

2) Use GcpPubSubHeaders.ORIGINAL_MESSAGE header to ack/nack manually (docs)