Spring集成http:入站通道适配器错误通道

时间:2016-07-22 10:41:29

标签: spring spring-integration

我最近从Spring Integration 4.1.3更新到4.2.6,并注意到我开始从http:inbound-gateway获得500响应。 经过调查,这是由于回复为空,网关将此解释为MessagingGatewaySupport中的超时

if (reply == null && this.errorOnTimeout) {

这是有道理的,所以我将其更改为http:inbound-channel-adapter并解决了这个问题,但错误处理并没有按预期运行。

我以前在网关上有

和错误频道
<int-http:inbound-gateway id="inboundGateway" request-channel="httpInChannel" reply-channel="httpResponseChannel" path="/**/status*" supported-methods="POST" error-channel="httpErrorChannel"/>
<int:chain input-channel="httpInChannel" output-channel="serviceChannel">
...
</int:chain>
<int:chain input-channel="httpErrorChannel">
    <int:header-enricher>
        <int:header name="http_statusCode" value="500" />
    </int:header-enricher>
    <int:transformer expression="payload.localizedMessage" />
</int:chain>
<int:service-activator input-channel="serviceChannel" ref="someController" method="someVoidMethod"/>

我怀疑它可能不起作用,但我稍微修改了

<int-http:inbound-channel-adapter id="inboundAdapter" channel="httpInChannel" path="/**/status*" supported-methods="POST" error-channel="httpErrorChannel"/>
<int:chain input-channel="httpInChannel" output-channel="serviceChannel">
    ...
</int:chain>
<int:chain input-channel="httpErrorChannel">
    <int:header-enricher>
        <int:header name="http_statusCode" value="500" />
    </int:header-enricher>
    <int:transformer expression="payload.localizedMessage" />
</int:chain>
<int:service-activator input-channel="serviceChannel" ref="someController" method="someVoidMethod"/>

现在,它对于正常的有效POST请求工作正常,但是如果我发送了一条无效的错误信息,我会得到500响应并且响应完整的错误堆栈(尝试更改标题更丰富的状态代码)。错误是

org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available

这是有道理的,因为error-channel没有输出(虽然它在文档中没有http://docs.spring.io/spring-integration/docs/4.3.0.RELEASE/reference/htmlsingle/#http-response-statuscode)。 有没有办法以类似于网关的方式更改入站适配器的错误响应?

1 个答案:

答案 0 :(得分:1)

我认为这是一个错误,请打开JIRA Issue

作为一种解决方法,在主流程中,在适配器之后,添加一个中流网关...

<int:service-activator ref="gw" input-channel="httpInChannel" >

<int:gateway id="gw" error-channel="httpErrorChannel"
     default-request-channel="nextChannelInMainFlow" default-reply-timeout="0" />

网关就像流程中的try / catch块一样。

回复超时非常重要,因为您不希望收到回复。