简体   繁体   English

Spring 集成:如何按需使用 QueueChannel 作为背压感知反应 (Flux) 管道

[英]Spring Integration: How to consume QueueChannel on-demand as backpressure aware reactive (Flux) pipeline

I have Spring Integration QueueChannel that I want to consume in backpressure aware Flux pipeline.我有 Spring Integration QueueChannel,我想在背压感知 Flux 管道中使用它。

  1. Prefetch n Messages from the queue.从队列中预取n条消息。
  2. Make async calls to external system like fun remoteCall(message: Message): Mono<Void> .对外部系统进行异步调用,例如fun remoteCall(message: Message): Mono<Void>
  3. Pull next message(s) from the queue after external calls will complete.在外部调用完成后从队列中拉下一条消息。

I don't want to use Poller with scheduler to pull messages from the queue ahead of time.我不想使用 Poller 和调度程序提前从队列中提取消息。

What is the best way to do it in latest Spring Integration + Java/Kotlin DSL, with error recovery etc?在最新的 Spring 集成 + Java/Kotlin DSL 中使用错误恢复等的最佳方法是什么?

See IntegrationReactiveUtils.messageChannelToFlux() :请参阅IntegrationReactiveUtils.messageChannelToFlux()

/**
 * Adapt a provided {@link MessageChannel} into a {@link Flux} source:
 * - a {@link org.springframework.integration.channel.FluxMessageChannel}
 * is returned as is because it is already a {@link Publisher};
 * - a {@link SubscribableChannel} is subscribed with a {@link MessageHandler}
 * for the {@link Sinks.Many#tryEmitNext(Object)} which is returned from this method;
 * - a {@link PollableChannel} is wrapped into a {@link MessageSource} lambda and reuses
 * {@link #messageSourceToFlux(MessageSource)}.
 * @param messageChannel the {@link MessageChannel} to adapt.
 * @param <T> the expected payload type.
 * @return a {@link Flux} which uses a provided {@link MessageChannel} as a source for events to publish.
 */
@SuppressWarnings("unchecked")
public static <T> Flux<Message<T>> messageChannelToFlux(MessageChannel messageChannel) {

Then you can use an IntegrationFlows.from(Publisher) :然后您可以使用IntegrationFlows.from(Publisher)

/**
 * Populate a {@link FluxMessageChannel} to the {@link IntegrationFlowBuilder} chain
 * and subscribe it to the provided {@link Publisher}.
 * @param publisher the {@link Publisher} to subscribe to.
 * @return new {@link IntegrationFlowBuilder}.
 */
public static IntegrationFlowBuilder from(Publisher<? extends Message<?>> publisher) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM