简体   繁体   English

Spring 内部的 Apache Camel 可以从 Kafka 获取以前的消息吗?

[英]Can Apache Camel inside Spring get previous messages from Kafka?

I have a Spring Boot application, which consumes messages from Apache Kafka through Apache Camel我有一个 Spring 启动应用程序,它使用来自 Apache Kafka 到 Apache Camel 的消息

public void init() throws Exception {
    DefaultCamelContext camelContext = new DefaultCamelContext();
    camelContext.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {
            from("kafka:destination?brokers=<host>:9092&maxBlockMs=5000&reconnectBackoffMaxMs=2000")
                    .process(senderProcessor);
        }
    });

    camelContext.start();
}

It works if the context is already started when messages arrive.如果消息到达时上下文已经启动,它就可以工作。 But if messages were sent to Kafka before the application was started, the app doesn't consume those messages.但是,如果在应用程序启动之前将消息发送到 Kafka,则应用程序不会使用这些消息。 I'm sure there must be the way to get them.我相信一定有办法得到它们。 Is it true?这是真的吗?

There is a Kafka consumer setting called auto.offset.reset and its default value is latest .有一个名为auto.offset.resetKafka 消费者设置,其默认值为latest

InCamel-Kafka the setting is named autoOffsetReset .Camel-Kafka中,该设置被命名为autoOffsetReset

That means, that the consumer ignores all existing messages and only consumes new ones.这意味着,消费者忽略所有现有的消息,只消费新的消息。 If you set it to earliest , the consumer consumes all existing messages.如果您将其设置为earliest ,消费者将使用所有现有消息。

Notice that this only matters when the consumer connects for the first time .请注意,这仅在消费者第一次连接时才重要 Once it is connected and consumes messages, it should commit the offset back to the broker and use this saved offset whenever it reconnects.一旦它连接并消费消息,它应该将偏移量提交回代理,并在重新连接时使用这个保存的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM