这种在 Spring Boot 应用程序中启动无限循环的方式有什么问题吗? [英] Are there any problems with this way of starting an infinite loop in a Spring Boot application?
问题描述
我有一个 Spring Boot 应用程序,它需要处理一些 Kafka 流数据.我向将在启动时运行的 CommandLineRunner
类添加了一个无限循环.其中有一个可以被唤醒的 Kafka 消费者.我用 Runtime.getRuntime().addShutdownHook(new Thread(consumer::wakeup));
添加了一个关闭钩子.我会遇到任何问题吗?在 Spring 中是否有更惯用的方式来做到这一点?我应该改用 @Scheduled
吗?下面的代码去除了特定的 Kafka 实现内容,但其他方面是完整的.
I have a Spring Boot application and it needs to process some Kafka streaming data. I added an infinite loop to a CommandLineRunner
class that will run on startup. In there is a Kafka consumer that can be woken up. I added a shutdown hook with Runtime.getRuntime().addShutdownHook(new Thread(consumer::wakeup));
. Will I run into any problems? Is there a more idiomatic way of doing this in Spring? Should I use @Scheduled
instead? The code below is stripped of specific Kafka-implementation stuff but otherwise complete.
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.errors.WakeupException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Properties;
@Component
public class InfiniteLoopStarter implements CommandLineRunner {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void run(String... args) {
Consumer<AccountKey, Account> consumer = new KafkaConsumer<>(new Properties());
Runtime.getRuntime().addShutdownHook(new Thread(consumer::wakeup));
try {
while (true) {
ConsumerRecords<AccountKey, Account> records = consumer.poll(Duration.ofSeconds(10L));
//process records
}
} catch (WakeupException e) {
logger.info("Consumer woken up for exiting.");
} finally {
consumer.close();
logger.info("Closed consumer, exiting.");
}
}
}
推荐答案
我不确定您是否会在那里遇到任何问题,但它有点脏 - Spring 对使用 Kafka 提供了非常好的内置支持,所以我会倾向于那个(网上有很多关于它的文档,但一个很好的文档是:https://www.baeldung.com/spring-kafka).
I'm not sure if you'll run into any issues there but it's a bit dirty - Spring has really nice built in support for working with Kafka so I would lean towards that (there's plenty of documentation on that on the web, but a nice one is: https://www.baeldung.com/spring-kafka).
您需要以下依赖项:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
配置就像将 @EnableKafka
注释添加到配置类,然后设置 Listener 和 ConsumerFactory bean 一样简单
Configuration is as easy adding the @EnableKafka
annotation to a config class and then setting up Listener and ConsumerFactory beans
配置完成后,您可以轻松设置消费者,如下所示:
Once configured you can setup a consumer easily as follows:
@KafkaListener(topics = "topicName")
public void listenWithHeaders(
@Payload String message,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition) {
System.out.println("Received Message: " + message"+ "from partition: " + partition);
}
这篇关于这种在 Spring Boot 应用程序中启动无限循环的方式有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!