[英]Apache Camel Kafka - aggregate kafka messages and publish to a different topic at regular intervals
我有一个用例:
我需要定期阅读和聚合来自kafka主题的消息,并发布到不同的主题。 Localstorage不是一种选择。 这就是我计划解决这个问题的方法,欢迎任何改进建议
要安排kafka消息的聚合和发布,请计划使用Aggregator EIP的completionInterval选项。 这是代码。
@Autowired ObjectMapper objectMapper;
JacksonDataFormat jacksonDataFormat;
@PostConstruct
public void initialize(){
//objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
jacksonDataFormat = new JacksonDataFormat(objectMapper,EventMessage.class);
}
和路线:
public void configure() throws Exception {
from("kafka:localhost:9092?topic=item-events" +
"&groupId=aggregator-group-id&autoCommitIntervalMs=25000&autoOffsetReset=earliest&consumersCount=1")
.routeId("kafkapoller")
.unmarshal(jacksonDataFormat)
.aggregate(body().method("getItemId"), new EventAggregationStrategy()).completionInterval(20000)
.marshal().json(JsonLibrary.Jackson)
.to("kafka:localhost:9092?topic=item-events-aggregated&serializerClass=org.apache.kafka.common.serialization.ByteArraySerializer");
}
这看起来不错。 要记住的事情:
PersistentAggregationRepository
来存储/重播消息,虽然你可以重播你从kafka丢失的消息(这将是我最大的操作问题) AggregateController
让你在外部强制完成交换,所以你可以做一些事情,比如发出关闭到camel然后调用它来完成机上交换
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.