Manual Acknowledgement of Messages : Spring Cloud Stream Kafka

岁酱吖の 提交于 2019-12-06 11:34:55

问题


The scenario i want to implement is consume a message from Kafka , process it, if some condition fails i do not wish to acknowledge the message. For this i found in the spring cloud stream reference documentation,

autoCommitOffset Whether to autocommit offsets when a message has been processed. If set to false, an Acknowledgment header will be available in the message headers for late acknowledgment.

Default: true.

My question is after setting autoCommitOffset to false, how can i acknowledge a message? A Code example would be hugely appreciated.


回答1:


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();
        }
    }
}


来源:https://stackoverflow.com/questions/37822252/manual-acknowledgement-of-messages-spring-cloud-stream-kafka

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!