【发布时间】:2022-01-21 15:39:26
【问题描述】:
我想定义一个使用 Reactor Kafka 消耗 kafka 并写入 MongoDB 的流,并且只有在成功时才会将 ID 写入 Kafka。我正在使用带有 Spring Integration JavaDSL 的 Project Reactor,并且我希望有一个 FlowBuilder 类来定义我的高级管道。我目前有以下方向:
public IntegrationFlow buildFlow() {
return IntegrationFlows.from(reactiveKafkaConsumerTemplate)
.publishSubscribeChannel(c -> c
.subscribe(sf -> sf
.handle(MongoDb.reactiveOutboundChannelAdapter()))
.handle(writeToKafka)
.get();
}
我见过docs that there is a support for a different approach, that also works with Project Reactor。这种方法不包括使用IntegrationFlows。这看起来像这样:
@MessagingGateway
public static interface TestGateway {
@Gateway(requestChannel = "promiseChannel")
Mono<Integer> multiply(Integer value);
}
...
@ServiceActivator(inputChannel = "promiseChannel")
public Integer multiply(Integer value) {
return value * 2;
}
...
Flux.just("1", "2", "3", "4", "5")
.map(Integer::parseInt)
.flatMap(this.testGateway::multiply)
.collectList()
.subscribe(integers -> ...);
我想知道在使用这两个库时更推荐的处理方式。我想知道如何在第二个示例中使用 Reactive MongoDB 适配器。如果没有 IntegrationFlows 包装器,我不确定第二种方法是否可行。
【问题讨论】:
标签: spring spring-boot spring-integration project-reactor reactor-kafka