為端點新增行為

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

例如,考慮以下流程

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://127.0.0.1/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/> 端點。結構描述不允許 <request-handler-advice-chain> 作為鏈本身的子元素。

然而,<request-handler-advice-chain> 可以新增至 <chain> 元素內的個別產生回覆的端點。例外情況是,在不產生回覆的鏈中,由於鏈中的最後一個元素是 outbound-channel-adapter,因此無法建議最後一個元素。如果您需要建議這類元素,則必須將其移至鏈外部(鏈的 output-channel 作為配接器的 input-channel)。然後可以像往常一樣建議配接器。對於產生回覆的鏈,可以建議每個子元素。