Spring Integration& Kafka Consumer:在成功获取记录后立即停止消息驱动通道适配器

时间:2017-04-12 01:41:24

标签: java spring spring-integration spring-xd spring-kafka

我正在使用以下内容:

  • spring-integration-kafka 2.1.0.RELEASE
  • kafka-clients 0.10.0.1
  • Kafka 0.10.x.x
  • 弹簧XD-1.3.1.RELEASE

我为SpringXD创建了自定义Kafka-source模块。我设置了我的消费者逻辑和我的message-driven-channel-adapter(我与control-bus一起使用来停止我的频道适配器)。到现在为止还挺好。我也使用kafka属性max.poll.record=10来获取每个轮询10条记录。

我想确保在所有记录(在本例中为10条记录)成功获取后立即停止我的频道。

所以例如:我想避免当没有成功获取和处理所有记录时(即,没有将记录发送到输出通道时)停止读取。

有没有办法说出来?

这是我的xml配置,以防万一:

xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
                               http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd">


<int:channel id="input_to_control_bus" />
<int:channel id="output" />

<context:component-scan base-package="com.kafka.source.logic" />


<int:control-bus id="my_control_bus" input-channel="input_to_control_bus" />

<int-kafka:message-driven-channel-adapter
    id="kafkaInboundChannelAdapterTesting" listener-container="container1"
    auto-startup="false" phase="100" send-timeout="5000" channel="output"
    mode="record" message-converter="messageConverter" />

<bean id="messageConverter"
    class="org.springframework.kafka.support.converter.MessagingMessageConverter" />

<!--Consumer -->
<bean id="container1"
    class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
    <constructor-arg>
        <bean class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
            <constructor-arg>
                <map>
                    <entry key="bootstrap.servers" value="localhost:9092" />
                    <entry key="enable.auto.commit" value="false" />
                    <entry key="auto.commit.interval.ms" value="100" />
                    <entry key="session.timeout.ms" value="15000" />
                    <entry key="max.poll.records" value="3" />
                    <entry key="group.id" value="bridge-stream-testing" />
                    <entry key="key.deserializer"
                        value="org.apache.kafka.common.serialization.IntegerDeserializer" />
                    <entry key="value.deserializer"
                        value="org.apache.kafka.common.serialization.StringDeserializer" />
                </map>
            </constructor-arg>
        </bean>
    </constructor-arg>

    <constructor-arg>
        <bean class="org.springframework.kafka.listener.config.ContainerProperties">
            <constructor-arg name="topics" value="testing-topic" />
        </bean>
    </constructor-arg>
</bean>

[更新N°1] 我为什么要这样做? 这些是细节:

  • 我想从Kafka主题每 Y 分钟阅读最多 X 条消息。
  • 我使用max.poll.records确保每次投票时最多只能获取 X 条消息。
  • 我想要处理的一个场景是:如果在一个特定的消息轮询中,我发送的消息少于 X 。这意味着我应该在不等待 X 消息的情况下停止频道,否则我将不得不等到将来的消息轮询才能到达 X 消息。

这些是关于这种情况的一些细节。有更多的场景,但我不想使用相同的SO问题混合它。

[更新N°2]

Artem回答后的一些想法。

  • 如果我没有定义max.poll.records并等到达 Y 分钟并计算 X 消息,然后{{1>频道?
  • 由于无法读取某些消息会丢失,或者当我再次stop频道时,会读取那些无法读取的消息吗?

我想避免将邮件留在内存中,这就是我使用start + message-driven-channel-adapter

的原因

1 个答案:

答案 0 :(得分:1)

我可以建议的是AtomicInteger bean,它会在每个已处理的记录中增加,当您达到阈值时,您会为stop()执行kafkaInboundChannelAdapterTesting