Spring Kafka Template implementaion example for se

2020-08-03 05:23发布

问题:

I am new to spring-kafka-template. I tried some basic stuff in it and they are working fine. But I am trying to implement some concepts mentioned at Spring Docs like :

  1. Offset Seeking
  2. Acknowledging listeners

I tried to find some example for it over net but was unsuccessful. The thing only I found is its source code.

We have a same issue as mentioned in this post Spring kafka consumer, seek offset at runtime.

But there is no example available to implement the same.

Can someone give any example on how to implement them?

Thanks in advance.

回答1:

You should use ConsumerSeekAware for that purpose to deal with seeks:

static class Listener implements ConsumerSeekAware {

     private final ThreadLocal<ConsumerSeekCallback> seekCallBack = new ThreadLocal<>();

     public void registerSeekCallback(ConsumerSeekCallback callback) {
        this.seekCallBack.set(callback);
    }

@KafkaListener(...)
        public void listen(@Payload String foo,
                Acknowledgment ack) {

                this.seekCallBack.get().seek(topic, partition, 0);
            }
        }

}