Spring Kafka : Subscribe to a new Topic Pattern during Runtime

孤人 提交于 2019-12-25 01:15:02

问题


I'm using the annotation @KafkaListener to consume topics in my application. I need to change the topic pattern at runtime in an already running consumer so that new topics that match the new pattern can be consumed.

I tried the below code, but it still consumes the topics matching the old topic pattern. Here, I have set the "old-topic-pattern" at application start-up. Then, I'm updating the pattern to "new-topic-pattern" every 10 seconds using a Spring @Scheduler.

Class "KafkaTopicPatternConfig.java":

@Configuration
public class KafkaTopicPatternConfig {

  @Bean
  public String kafkaTopicPattern(Environment env) {
    logger.info("Getting kafka topic pattern");
    String kafkaTopicPattern = "old-topic-pattern";
    return kafkaTopicPattern;
  }
}



Class "Consumer.java":

@Component
public class Consumer implements ConsumerSeekAware{

  @Autowired
  @Qualifier("kafkaTopicPattern")
  private String kafkaTopicPattern;


  @KafkaListener(topicPattern = "#{kafkaTopicPattern}", id = "s4federatorConsumer")
  public void processMessage(@Payload ConsumerRecord<String, Object> record,
        @Header(KafkaHeaders.OFFSET) Long offset,
        @Header(KafkaHeaders.CONSUMER) KafkaConsumer<String, String> consumer,
        @Header(KafkaHeaders.RECEIVED_PARTITION_ID) Integer partitionId) {

        //do something with the consumed message

  }


  @Scheduled(fixedDelay = 10000, initialDelay = 15000)
  public void refreshKafkaTopics() {
    logger.info("Inside scheduler to refresh kafka topics");
    this.kafkaTopicPattern = "new-topic-pattern";
    this.kafkaListenerEndpointRegistry.getListenerContainer("s4federatorConsumer").stop();
    this.kafkaListenerEndpointRegistry.getListenerContainer("s4federatorConsumer").start();
  }
}

回答1:


You are getting kafkaTopicPattern as -

@Qualifier("kafkaTopicPattern")
private String kafkaTopicPattern;

I see you are updating the pattern like -

this.kafkaTopicPattern = "new-topic-pattern";

But the original value for "kafkaTopicPattern" which is injected in listener wont be refreshed by this if these 2 are in different instance objects. So you will have to make sure that the listener objects are refreshed with the new pattern.



来源:https://stackoverflow.com/questions/56523857/spring-kafka-subscribe-to-a-new-topic-pattern-during-runtime

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