我正在使用以下内容:
我为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] 我为什么要这样做? 这些是细节:
max.poll.records
确保每次投票时最多只能获取 X 条消息。这些是关于这种情况的一些细节。有更多的场景,但我不想使用相同的SO问题混合它。
[更新N°2]
Artem回答后的一些想法。
max.poll.records
并等到达 Y 分钟并计算 X 消息,然后{{1>频道?stop
频道时,会读取那些无法读取的消息吗?我想避免将邮件留在内存中,这就是我使用start
+ message-driven-channel-adapter
答案 0 :(得分:1)
我可以建议的是AtomicInteger
bean,它会在每个已处理的记录中增加,当您达到阈值时,您会为stop()
执行kafkaInboundChannelAdapterTesting
。