是否有读取 Kafka 主题的 Spring Schedule 示例? [英] Is there any example of Spring Schedule that reads Kafka topic?

查看:86
本文介绍了是否有读取 Kafka 主题的 Spring Schedule 示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图在指定的窗口时间从 Kafka 读取数据(所以我们有 Kafka 消费者),这意味着避免在其他时间读取数据.但是,我们不确定如何在时间段到期后关闭消费者.我想知道是否有任何示例可以说明如何做到这一点?非常感谢您帮助我们.

We're trying to read the data from Kafka at specified window time (so we have Kafka consumer), that means avoiding the data read at other times. However we're not sure how to shut down the consumer after the time period is expired. I wonder if there are any example of how to do that? Many thanks in advance for helping us.

推荐答案

您可以禁用 autoStartup,然后使用 KafkaListenerEndpointRegistry start停止方法@KafkaListener 生命周期管理

You can disable the autoStartup and then manually start the kafka containers using KafkaListenerEndpointRegistry start and stop methods @KafkaListener Lifecycle Management

public class KafkaConsumer {

 @Autowired
 private KafkaListenerEndpointRegistry registry;

  @KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false")
  public void listen(...) { ... }

  @Schedule(cron = "")
  public void scheduledMethod() {

   registry.start();

   registry.stop()
  }

但是在上面的方法中,不能保证在那个时间范围内来自 kafka 的所有消息都会被消耗掉(这取决于负载和处理速度)

But in the above approach there is no guarantee that all messages from kafka will be consumed in that time frame (It depends on load and processing speed)

这篇关于是否有读取 Kafka 主题的 Spring Schedule 示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆