【问题标题】:Spring Integration inbound-gateway reply-channel has no subscribers for channelSpring Integration inbound-gateway reply-channel 没有频道订阅者
【发布时间】:2016-02-26 06:50:32
【问题描述】:

我在这里有一个简单的“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 org.springframework.integration.endpoint.EventDrivenConsumer@4eef7503

看起来 Spring 在最后一分钟添加了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
'org.springframework.context.support.GenericApplicationContext@64485a47.myReplyChannel'.

这对我来说就像我的配置错误,而这里 Spring 没有牵着我的手。

替代配置:

我尝试将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

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

    将 {bridge:null} 添加为“myReplyChannel”频道的订阅者

    .

    调试这个

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

    如果您确实指定了回复通道,网关会在内部创建一个桥接器,以便专门发送的任何回复都会桥接到请求的 replyChannel 标头。

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

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

    如果您想在测试代码中模拟 HTTP 网关,请使用消息网关,或仅使用 MessagingTemplate.convertSendAndReceive() - 任何一种都将正确设置请求消息中的 replyChannel 标头。

    或者,使用:

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

    每个请求都需要自己的回复通道标头,因此我们知道如何将回复路由到正确的请求线程。

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2017-05-05
      • 2021-01-31
      • 1970-01-01
      • 2019-10-04
      • 2018-06-22
      • 2018-09-17
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多