2016-02-26 80 views
0

我有一个简单的“Hello World”示例工作流程,在这里我想公开一个以纯文本响应的inbound-gateway Web服务。我相信我将回应路由到myReplyChannel的方式不正确。春季集成入站网关回复通道没有用户通道

<int:channel id="myRequestChannel"/> 
<int:channel id="myReplyChannel"/> 

<int-http:inbound-gateway id="myGateway" 
          path="/hi" 
          supported-methods="GET" 
          request-channel="myRequestChannel" 
          reply-channel="myReplyChannel"/> 

<int:transformer input-channel="myRequestChannel" 
       output-channel="myReplyChannel" 
       expression="'Hello World!'"/> 

这工作部署时,但是当我第一次叫我看这个服务记录:

Adding {bridge:null} as a subscriber to the 'myReplyChannel' channel 
Channel 'org.springframework.web.context.WebApplicationContext:myReplyChannel' has 1 subscriber(s). 
started [email protected]f7503 

看起来像春天在用户在最后一刻加入了myReplyChannel。我宁愿自己正确地做。

单元测试

我写了一个简单的单元测试调试这个..

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:hello.xml" }) 
public class HelloWorldTest { 

    @Autowired 
    private MessageChannel myRequestChannel; 

    @Test 
    public void test() { 
     myRequestChannel.send(MessageBuilder.withPayload("").build()); 
    } 

} 

这个错误出具有:

org.springframework.messaging.MessageDeliveryException: 
    Dispatcher has no subscribers for channel 
'[email protected]485a47.myReplyChannel'. 

这读对我来说,我的配置是错了,这里春天不牵着我的手。

备用配置:

我试过只是删除myReplyChannel一起,它的工作没有在日志中任何东西。

<int:channel id="myRequestChannel"/> 

<int-http:inbound-gateway id="myGateway" 
          path="/ok" 
          supported-methods="GET" 
          request-channel="myRequestChannel"/> 

<int:transformer input-channel="myRequestChannel" expression="'OK'"/> 

这是正确的设置?如果是这样,参数reply-channel是什么?

有了这个配置,我得到了我的单元测试以下错误:

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

回答

0

添加{桥:空}作为用户的 'myReplyChannel' 通道

调试这个

没什么好 “调试”。这只是框架内部的DEBUG消息。每个请求获得专用的replyChannel标题。通常,网关上不需要reply-channel;当框架到达某个没有output-channel的组件时(如第二次测试中发现的),该框架将自动路由到此请求的回复通道头。

如果指定的答复道,网关将创建一个桥内部,这样任何答复专门派有桥接到请求的replyChannel头。

通常情况下,指定回复频道的唯一原因是,如果您想要对回复做其他操作(例如,请点击频道以记录回复,或将频道设为发布 - 订阅频道,以便您可以在其他地方发送答复的副本)。

您的测试失败,因为您没有像网关那样填充replyChannel头。

如果要在测试代码中模拟HTTP网关,请使用消息传递网关,或者只需使用MessagingTemplate.convertSendAndReceive()-要么在请求消息中正确设置replyChannel标头。

另外,使用:

myRequestChannel.send(MessageBuilder.withPayload("") 
          .setReplyChannel(new QueueChannel()) 
          .build()); 

每个请求都需要自己的答复通道头,所以我们知道如何路由请求线程的答复的权利。