向端點新增行為

在 Spring Integration 2.2 之前,您可以透過向輪詢器的 <advice-chain/> 元素新增 AOP Advice 來向整個整合流新增行為。然而,假設您只想重試一個 REST Web Service 呼叫,而不是下游的任何端點。

例如,考慮以下流程

inbound-adapter->poller->http-gateway1->http-gateway2->jdbc-outbound-adapter

如果您在輪詢器的 advice 鏈中配置了一些重試邏輯,並且對 http-gateway2 的呼叫由於網路故障而失敗,那麼重試會導致 http-gateway1http-gateway2 都被第二次呼叫。同樣,在 jdbc-outbound-adapter 中發生瞬時故障後,兩個 HTTP 閘道器都會被第二次呼叫,然後再次呼叫 jdbc-outbound-adapter

Spring Integration 2.2 添加了向單個端點新增行為的能力。這是透過在許多端點中新增 <request-handler-advice-chain/> 元素來實現的。以下示例展示瞭如何在 outbound-gateway 中使用 <request-handler-advice-chain/> 元素

<int-http:outbound-gateway id="withAdvice"
    url-expression="'https:///test1'"
    request-channel="requests"
    reply-channel="nextChannel">
    <int-http:request-handler-advice-chain>
        <ref bean="myRetryAdvice" />
    </int-http:request-handler-advice-chain>
</int-http:outbound-gateway>

在這種情況下,myRetryAdvice 僅區域性應用於此閘道器,並且不適用於回覆傳送到 nextChannel 後下遊採取的進一步操作。advice 的範圍僅限於端點本身。

目前,您不能為整個端點 <chain/> 新增 advice。schema 不允許將 <request-handler-advice-chain> 作為 chain 本身的子元素。

然而,可以將 <request-handler-advice-chain> 新增到 <chain> 元素中產生回覆的單個端點。一個例外是,在不產生回覆的 chain 中,由於 chain 中的最後一個元素是 outbound-channel-adapter,該最後一個元素無法被新增 advice。如果您需要為這樣的元素新增 advice,則必須將其移出 chain(將 chain 的 output-channel 作為 adapter 的 input-channel)。然後可以像往常一樣為該 adapter 新增 advice。對於產生回覆的 chain,每個子元素都可以被新增 advice。