訊息橋接

訊息橋接是一個相對簡單的端點,它連線兩個訊息通道或通道介面卡。例如,您可能希望將 PollableChannel 連線到 SubscribableChannel,以便訂閱端點無需擔心任何輪詢配置。相反,訊息橋接提供了輪詢配置。

透過在兩個通道之間提供一箇中間輪詢器,您可以使用訊息橋接來限制入站訊息。輪詢器的觸發器決定了訊息到達第二個通道的速率,輪詢器的 maxMessagesPerPoll 屬性強制限制了吞吐量。

訊息橋接的另一個有效用途是連線兩個不同的系統。在這種情況下,Spring Integration 的作用僅限於在這些系統之間建立連線並在必要時管理一個輪詢器。更常見的情況是,在兩個系統之間至少有一個轉換器,用於在它們的資料格式之間進行轉換。在這種情況下,通道可以作為轉換器端點的“input-channel”和“output-channel”提供。如果不需要資料格式轉換,訊息橋接可能確實足夠了。

使用 XML 配置橋接

您可以使用 <bridge> 元素在兩個訊息通道或通道介面卡之間建立訊息橋接。為此,請提供 input-channeloutput-channel 屬性,如以下示例所示

<int:bridge input-channel="input" output-channel="output"/>

如上所述,訊息橋接的一個常見用例是連線 PollableChannelSubscribableChannel。當扮演這個角色時,訊息橋接也可能充當節流器

<int:bridge input-channel="pollable" output-channel="subscribable">
     <int:poller max-messages-per-poll="10" fixed-rate="5000"/>
 </int:bridge>

您可以使用類似的機制連線通道介面卡。以下示例展示了 Spring Integration 的 stream 名稱空間中 stdinstdout 介面卡之間的簡單“回顯”

<int-stream:stdin-channel-adapter id="stdin"/>

 <int-stream:stdout-channel-adapter id="stdout"/>

 <int:bridge id="echo" input-channel="stdin" output-channel="stdout"/>

類似的配置適用於其他(可能更有用)通道介面卡橋接,例如檔案到 JMS 或郵件到檔案。即將到來的章節將介紹各種通道介面卡。

如果橋接上未定義“output-channel”,則使用入站訊息提供的回覆通道(如果可用)。如果輸出和回覆通道均不可用,則會丟擲異常。

使用 Java 配置橋接

以下示例展示瞭如何使用 @BridgeFrom 註解在 Java 中配置橋接

@Bean
public PollableChannel polled() {
    return new QueueChannel();
}

@Bean
@BridgeFrom(value = "polled", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public SubscribableChannel direct() {
    return new DirectChannel();
}

以下示例展示瞭如何使用 @BridgeTo 註解在 Java 中配置橋接

@Bean
@BridgeTo(value = "direct", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public PollableChannel polled() {
    return new QueueChannel();
}

@Bean
public SubscribableChannel direct() {
    return new DirectChannel();
}

或者,您可以使用 BridgeHandler,如以下示例所示

@Bean
@ServiceActivator(inputChannel = "polled",
        poller = @Poller(fixedRate = "5000", maxMessagesPerPoll = "10"))
public BridgeHandler bridge() {
    BridgeHandler bridge = new BridgeHandler();
    bridge.setOutputChannelName("direct");
    return bridge;
}

使用 Java DSL 配置橋接

您可以使用 Java 領域特定語言(DSL)來配置橋接,如以下示例所示

@Bean
public IntegrationFlow bridgeFlow() {
    return IntegrationFlow.from("polled")
            .bridge(e -> e.poller(Pollers.fixedDelay(5000).maxMessagesPerPoll(10)))
            .channel("direct")
            .get();
}
© . This site is unofficial and not affiliated with VMware.