Adding Behavior to Endpoints

在 Spring Integration 2.2 之前,你可以在轮询器的 <advice-chain/> 元素中添加 AOP 建议来向整个集成流添加行为。然而,假设你想要重试(例如,仅重试 REST Web 服务调用,而不是任何下游端点)。

Prior to Spring Integration 2.2, you could add behavior to an entire Integration flow by adding an AOP Advice to a poller’s <advice-chain/> element. However, suppose you want to retry, say, just a REST Web Service call, and not any downstream endpoints.

例如,考虑以下流:

For example, consider the following flow:

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

如果你在轮询器的建议链中配置一些重试逻辑,而对 http-gateway2 的调用由于网络故障而失败,则重试会导致 http-gateway1http-gateway2 被第二次调用。类似地,在 jdbc-outbound-adapter 中出现暂时故障后,两个 HTTP 网关都会被二次调用,然后再调用 jdbc-outbound-adapter

If you configure some retry-logic into an advice chain on the poller and the call to http-gateway2 failed because of a network glitch, the retry causes both http-gateway1 and http-gateway2 to be called a second time. Similarly, after a transient failure in the jdbc-outbound-adapter, both HTTP gateways are called a second time before again calling the jdbc-outbound-adapter.

Spring Integration 2.2 添加了向单个端点添加行为的功能。这是通过向许多端点添加 <request-handler-advice-chain/> 元素来实现的。以下示例显示了要在 outbound-gateway 中使用 <request-handler-advice-chain/> 元素的方法:

Spring Integration 2.2 adds the ability to add behavior to individual endpoints. This is achieved by the addition of the <request-handler-advice-chain/> element to many endpoints. The following example shows how to the <request-handler-advice-chain/> element within an outbound-gateway:

<int-http:outbound-gateway id="withAdvice"
    url-expression="'http://localhost/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 后采取的进一步操作。建议的范围仅限于端点本身。

In this case, myRetryAdvice is applied only locally to this gateway and does not apply to further actions taken downstream after the reply is sent to nextChannel. The scope of the advice is limited to the endpoint itself.

此时,你无法建议整个 <chain/> 端点。架构不允许将 <request-handler-advice-chain> 作为 chain 本身的子元素。

At this time, you cannot advise an entire <chain/> of endpoints. The schema does not allow a <request-handler-advice-chain> as a child element of the chain itself.

但是,可以在 <chain> 元素内将 <request-handler-advice-chain> 添加到各个产生回复的端点中。例外情况是,在不产生任何回复的链中,由于链中的最后一个元素是 outbound-channel-adapter,因此无法建议该最后一个元素。如果您需要建议这样的元素,则必须将它移出链之外(链的 output-channel 是适配器的 input-channel)。然后可以照常建议适配器。对于产生回复的链,可以建议每个子元素。

However, a <request-handler-advice-chain> can be added to individual reply-producing endpoints within a <chain> element. An exception is that, in a chain that produces no reply, because the last element in the chain is an outbound-channel-adapter, that last element cannot be advised. If you need to advise such an element, it must be moved outside the chain (with the output-channel of the chain being the input-channel of the adapter). The adapter can then be advised as usual. For chains that produce a reply, every child element can be advised.