我想知道spring-kafka
中是否有某些选项可以将所有新消息都捕获到列表中。
例如,如果我正在监听Message
对象,那么我想获取自上次民意调查以来的List<Message>
。像这样:
@KafkaListener(poll-interval=1000, topics = "${kafka.topic}", containerFactory = "objectListListenerContainerFactory", )
public void messageListener(List<Message> messages) {
log.info("Count of new messages since last poll : {}", messages.size());
}
我已经经历过Spring Kafka: Poll for new messages instead of being notified using `onMessage`。但是对我来说不是很有用。
答案 0 :(得分:1)
使用普通的Spring Kafka,您可以使用XmlDocument xdoc = new XmlDocument("YourXmlString");
XmlNodeList xNodes = xdoc.GetElementsByTagName("Id");
string Wished = xNodes[0].InnerText;
创建一个ConsumerFactory
,然后您可以自己方便地从那里轮询记录。
还请注意,KafkaConsumer
可以在任意时刻被暂停以停止轮询,但仍可以连接到消费者组。
在Spring Integration Kafka扩展中,有一个新的KafkaMessageListenerContainer
用于此类任务:
KafkaMessageSource
我认为我们需要考虑在Spring for Apache Kafka参考手册https://docs.spring.io/spring-kafka/docs/current/reference/html/_spring_integration.html#si-kafka中对此文档进行记录。因此,随时可以在/**
* Polled message source for kafka. Only one thread can poll for data (or
* acknowledge a message) at a time.
* <p>
* NOTE: If the application acknowledges messages out of order, the acks
* will be deferred until all messages prior to the offset are ack'd.
* If multiple records are retrieved and an earlier offset is requeued, records
* from the subsequent offsets will be redelivered - even if they were
* processed successfully. Applications should therefore implement
* idempotency.
*
* @param <K> the key type.
* @param <V> the value type.
*
* @author Gary Russell
* @author Mark Norkin
* @author Artem Bilan
*
* @since 3.0.1
*
*/
public class KafkaMessageSource<K, V> extends AbstractMessageSource<Object>
implements DisposableBean, Lifecycle {
项目中提出一个问题来解决这一差距。