向终端节点添加行为

在 Spring Integration 2.2 之前,你可以通过将 AOP Advice 添加到 Poller 的<advice-chain/>元素。 但是,假设您只想重试 REST Web 服务调用,而不重试任何下游终端节点。spring-doc.cadn.net.cn

例如,请考虑以程:spring-doc.cadn.net.cn

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

如果你在 Poller 上将一些 retry-logic 配置到通知链中,并且调用http-gateway2由于网络故障而失败,则重试会导致http-gateway1http-gateway2以第二次调用。 同样,在 jdbc-outbound-adapter 中出现暂时性故障后,两个 HTTP 网关都会被第二次调用,然后再次调用jdbc-outbound-adapter.spring-doc.cadn.net.cn

Spring Integration 2.2 增加了向单个端点添加行为的能力。 这是通过添加<request-handler-advice-chain/>元素添加到多个端点。 以下示例显示了如何<request-handler-advice-chain/>元素中outbound-gateway:spring-doc.cadn.net.cn

<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. 建议的范围仅限于终端节点本身。spring-doc.cadn.net.cn

此时,您无法建议整个<chain/>的端点。 架构不允许<request-handler-advice-chain>作为 Chain 本身的子元素。spring-doc.cadn.net.cn

但是,<request-handler-advice-chain>可以添加到<chain>元素。 一个例外是,在不产生回复的链中,因为链中的最后一个元素是outbound-channel-adapter,则无法建议最后一个元素。 如果需要通知这样的元素,则必须将其移动到链之外(使用output-channel链中为input-channel的适配器)。 然后可以像往常一样通知适配器。 对于生成 reply 的链,可以通知每个 child element。spring-doc.cadn.net.cn