沉浸在远程服务对发布者的http响应中

时间:2019-06-02 09:26:48

标签: spring-boot spring-integration spring-webflux spring-integration-dsl

我们的系统接收消息以从远程服务获取数据,然后将其存储到数据库中。当前,它打开与数据库的多个连接以保存每个请求的提取数据。我们希望将其转换为具有多个生产者(从远程服务获取数据)和单个使用者的流程,以将数据持久化在数据库中。这样做最多只能保持一个连接以将数据持久存储在数据库中。

我们正在使用带有反应堆的弹簧靴。我们希望有一个发布者发布从远程服务获取的所有数据,我们可以订阅这些数据并将其推送到数据库中的200条记录中。

例如,我正计划向我们提供以下代码来使用ActiveMQ队列中的消息:

    public Publisher<Message<RestoreMessage>> restoreMessagesSource() {
        return IntegrationFlows
            .from(Jms.messageDrivenChannelAdapter(this.connectionFactory)
                .destination(RestoreMessage.class.getSimpleName() + "Queue"))
            .channel(MessageChannels.queue())
            .log(LoggingHandler.Level.DEBUG)
            .log()
            .toReactivePublisher();
    }

在此代码中,来自ActiveMQ qeueu的消息被放入ReactivePublisher中。该发布者已被订阅。这样,我们就可以混淆队列中的消息。

以类似的方式,我们希望将所有远程API的响应推送到发布者,然后可以在一个位置上在订阅者中进行处理。

1 个答案:

答案 0 :(得分:0)

听起来像您将有多个Publisher<Message<?>>,并且您想将它们全部消耗在一个订户中。因此,您可以使用:

/**
 * Merge data from {@link Publisher} sequences contained in an array / vararg
 * into an interleaved merged sequence. Unlike {@link #concat(Publisher) concat},
 * sources are subscribed to eagerly.
 * <p>
 * <img class="marble" src="doc-files/marbles/mergeFixedSources.svg" alt="">
 * <p>
 * Note that merge is tailored to work with asynchronous sources or finite sources. When dealing with
 * an infinite source that doesn't already publish on a dedicated Scheduler, you must isolate that source
 * in its own Scheduler, as merge would otherwise attempt to drain it before subscribing to
 * another source.
 *
 * @param sources the array of {@link Publisher} sources to merge
 * @param <I> The source type of the data sequence
 *
 * @return a merged {@link Flux}
 */
@SafeVarargs
public static <I> Flux<I> merge(Publisher<? extends I>... sources) {

因此,您将把所有源都集中到一个Flux上,并将订阅该源。

请注意Note.toReactivePublisher()实际上产生了无限源,尽管根据Jms.messageDrivenChannelAdapter(),它是在侦听器容器中的执行程序的特定线程中完成的。因此,请按原样尝试,或将每个来源包装到具有特定Flux的{​​{1}}上。