繁体   English   中英

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

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

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

有没有一种方法可以“强制”使用指定的回复通道? Spring文件指出

通过定义默认回复渠道,您可以指向您选择的渠道,在这种情况下,这将是发布-订阅渠道。 网关将创建一个到该网关到存储在标头中的临时匿名回复通道的桥。

我尝试使用publish-subscribe-channel作为reply-channel ,但是没有任何区别。 也许我误会了这篇文章...

在我的链中,我还尝试了一个header-enricher 我想用我想拦截的通道的ID(submit.reply.channel)覆盖ReplyChannel的值。 在调试时,我能够在标题中看到“ 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>

在此先感谢您的帮助!

唯一“简便”的方法是通过最后一个端点上的output-channel显式发送答复。

实际上,将回复通道发送到已声明的通道时,所发生的一切只是简单地桥接到replyChannel标头。

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

编辑:

样本配置...

<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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM