向端点添加行为

在 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="'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 后下游采取的进一步操作。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。