Manual Acknowledgement of Messages : Spring Cloud Stream Kafka

[亡魂溺海] 提交于 2019-12-04 17:13:04

I've provided an answer to the question here https://github.com/spring-cloud/spring-cloud-stream/issues/575

Essentially it comes down to setting spring.cloud.stream.kafka.bindings.input.consumer.autoCommitOffset=false

and then handling the acknowledgment header:

@SpringBootApplication
@EnableBinding(Sink.class)
   public class ManuallyAcknowdledgingConsumer {

      public static void main(String[] args) {
         SpringApplication.run(ManuallyAcknowdledgingConsumer.class, args);
      }

      @StreamListener(Sink.INPUT)
      public void process(Message<?> message) {
         System.out.println(message.getPayload());
         Acknowledgment acknowledgment = message.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT, Acknowledgment.class);
        if (acknowledgment != null) {
           System.out.println("Acknowledgment provided");
           acknowledgment.acknowledge();
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!