拦截 Spring 集成 http:inbound-gateway 的 replyChannel

Intercept a Spring integration http:inbound-gateway's replyChannel

我想在 http:inbound-gatewayreply-channel 上应用拦截器,以将一些与事件相关的数据保存到 table。流程在 chain 中继续,然后转到 header-value-router。作为示例,让我们在此流程末尾使用 service-activator,其中未指定 output-channel。在这种情况下,replyChannel header 持有 TemporaryReplyChannel object(匿名回复通道)而不是网关的 reply-channel。这样拦截器就不会被调用。

有没有办法"force"使用指定的reply-channel? The Spring document 表示

by defining a default-reply-channel you can point to a channel of your choosing, which in this case would be a publish-subscribe-channel. The Gateway would create a bridge from it to the temporary, anonymous reply channel that is stored in the header.

我试过将 publish-subscribe-channel 用作 reply-channel,但没有任何区别。可能是我对文章的理解有误...

在我的链中,我还尝试了 header-enricher。我想用我想拦截的频道的 ID 覆盖 replyChannel 的值 (submit.reply.channel)。调试时,我能够在 header 中看到 "submit.reply.channel",但随后出现异常 java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/NoRollbackRuleAttribute 并停止尝试 ;-)

代码片段

<int-http:inbound-gateway id="submitHttpGateway"
    request-channel="submit.request.channel" reply-channel="submit.reply.channel" path="/submit" supported-methods="GET">
    <int-http:header name="requestAttributes" expression="#requestAttributes" />
    <int-http:header name="requestParametersMap" expression="#requestParams" />
</int-http:inbound-gateway>

<int:channel id="submit.request.channel" />
<int:publish-subscribe-channel id="submit.reply.channel">
    <int:interceptors>
        <int:ref bean="replyChannelInterceptor" />
    </int:interceptors>
</int:publish-subscribe-channel>

在此先感谢您的帮助!

唯一的 "easy" 方法是通过最后一个端点上的 output-channel 显式发送回复。

事实上,当您发送到已声明的频道时,所发生的只是回复频道被简单地桥接到 replyChannel header。

您可以通过将 replyChannel header 保存在另一个 header 中来实现,将 replyChannel header 设置为其他频道(您可以拦截);然后在回复返回到网关之前将 replyChannel header 恢复到 saved-off 通道。

编辑:

示例配置...

<int:channel id="in" />

<int:header-enricher input-channel="in" output-channel="next">
    <int:header name="origReplyChannel" expression="headers['replyChannel']"/>
    <int:reply-channel ref="myReplies" overwrite="true" />
</int:header-enricher>

<int:router input-channel="next" expression="payload.equals('foo')">
    <int:mapping value="true" channel="channel1" />
    <int:mapping value="false" channel="channel2" />
</int:router>

<int:transformer input-channel="channel1" expression="payload.toUpperCase()" />

<int:transformer input-channel="channel2" expression="payload + payload" />

<int:channel id="myReplies" />

<!-- restore the reply channel -->
<int:header-enricher input-channel="myReplies" output-channel="tapped">
    <int:reply-channel expression="headers['origReplyChannel']" overwrite="true" />
</int:header-enricher>

<int:channel id="tapped">
    <int:interceptors>
        <int:wire-tap channel="loggingChannel" />
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="loggingChannel" log-full-message="true" logger-name="tapInbound"
    level="INFO" />

<!-- route reply -->
<int:bridge id="bridgeToNowhere" input-channel="tapped" />

测试:

MessageChannel channel = context.getBean("in", MessageChannel.class);
MessagingTemplate template = new MessagingTemplate(channel);
String reply = template.convertSendAndReceive("foo", String.class);
System.out.println(reply);
reply = template.convertSendAndReceive("bar", String.class);
System.out.println(reply);  }

结果:

09:36:30.224 INFO  [main][tapInbound] GenericMessage [payload=FOO, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@fba92d3, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@fba92d3, id=326a610f-80c6-5b74-0158-e3644b732aab, origReplyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@fba92d3, timestamp=1442496990223}]
FOO
09:36:30.227 INFO  [main][tapInbound] GenericMessage [payload=barbar, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@662b4c69, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@662b4c69, id=d161917c-ca73-a5a9-d0f1-d7a4346a459e, origReplyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@662b4c69, timestamp=1442496990227}]
barbar