簡體   English   中英

將遠程服務的http響應下沉到發布者

[英]To sink in the http responses of a remote service to a publisher

我們的系統接收消息以從遠程服務獲取數據,然后將其存儲到數據庫中。 目前,它會打開與數據庫的多個連接,以保存每個請求的已獲取數據。 我們希望將其轉換為具有多個生成器(從遠程服務獲取數據)和單個使用者的進程,以在數據庫中保留數據。 這樣做它最多只能保持一個連接來保存數據庫中的數據。

我們正在使用帶反應器的彈簧靴。 我們希望發布者發布從我們可以訂閱的遠程服務獲取的所有數據,並將這些數據推送到數據庫中的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的響應推送到我們可以在一個地方的訂戶中處理的發布者。

聽起來你將有幾個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()它是在偵聽器容器中的執行器的特定線程中完成的。 因此,請按原樣嘗試或使用特定的publishOn()將每個源包裝到Flux

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM