kafka 订阅模式 java,Spring Kafka:在运行时订阅新的主题模式

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 record,

@Header(KafkaHeaders.OFFSET) Long offset,

@Header(KafkaHeaders.CONSUMER) KafkaConsumer 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();

}

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值