方法返回无效时的Spring Integration Gateway回复通道

时间:2015-10-18 14:57:39

标签: methods integration spring-integration void gateway

关于SI网关功能的问题/澄清:

如果我的网关接口定义如下:

public interface MyGateway{
   public void myGatewayMethod(Message<?> inMessage);
}

我的网关配置定义如下:

<int:gateway id="mySvcGateway"
                 service-interface="com.myCompany.myPkg.MyGateway"
                 error-channel="globalExceptionHandlerChannel">

        <int:method name="myGatewayMethod" request-channel="myGatewayReqChannel" />     
    </int:gateway>

我的问题/澄清是:

1)由于网关服务接口方法返回void,网关代理bean是否仍在&#34; default-reply-channel&#34;或用户定义的#34;回复频道&#34; ?

2)换句话说,我还需要提及reply-channel="nullChannel"(或default-reply-channel="nullChannel")吗?

OR

由于方法返回无效,网关会自动理解不听取回复频道吗?

3)我是否仍然可以将reply-timeout属性添加到此配置中,或者由于没有预期的回复而没有意义?

在类似的上下文中,如果我在服务接口方法中添加另一个方法,如下所示:

public interface MyGateway{
       public void myGatewayMethod(Message<?> inMessage);
       public Object myGatewayMethod2(Message<?> inMessage);
    }

并在我的网关配置中添加此方法,如下所示:

<int:gateway id="mySvcGateway"
                     service-interface="com.myCompany.myPkg.MyGateway"
                     error-channel="globalExceptionHandlerChannel">

            <int:method name="myGatewayMethod" request-channel="myGatewayReqChannel" /> 
<int:method name="myGatewayMethod2" request-channel="myGatewayReqChannel2" />   
        </int:gateway>

4)在这种情况下,我认为我需要定义reply-channel,对吗?

5)default-reply-channel可能不适用于这种情况,因为一个方法网关期望响应而不是其他方法,正确吗?

6)如果是,那么对于返回void的方法,我是否需要明确提及reply-channel="nullChannel"

感谢确认。

1 个答案:

答案 0 :(得分:5)

拉​​利特!

感谢大家提出这么多问题,我很惊讶所有这些问题都围绕着网关的void方法。

对所有这些人的快速合理答案是:

由于我们没有在参考手册中说明有关此事的任何事情,因此不必担心这样的配置,它应该按照预期的那样工作 对Spring Integration的信心。

我开玩笑说,但每个笑话都有一定道理。

现在让我们来看看GatewayProxyFactoryBean的源代码:

 private Object invokeGatewayMethod(MethodInvocation invocation, boolean runningOnCallerThread) throws Exception {
    ..........
    boolean shouldReply = returnType != void.class;
    ..................
        Object[] args = invocation.getArguments();
        if (shouldReply) {
            response = shouldReturnMessage ? gateway.sendAndReceiveMessage(args) : gateway.sendAndReceive(args);
        }
        else {
            gateway.send(args);
            response = null;
        }
    }
    return (response != null) ? this.convert(response, returnType) : null;
}

MessagingGatewaySupport.send()代表

的地方
this.messagingTemplate.convertAndSend(requestChannel, object, this.historyWritingPostProcessor);

也是void,最后只调用MessageChannel.send()

您可能认为此方法根本不关心replyChannelreplyTimeout

逻辑上,它假定void方法只会忽略这些选项,而其他方法的任何default-*都不会影响void返回类型的那些。

希望我很清楚。