5

为什么即使在我指定之后我也会收到以下异常requires-reply="false"

例外

org.springframework.integration.support.channel.ChannelResolutionException:没有可用的输出通道或replyChannel标头

配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd">

    <int:channel id="inChannel">

    </int:channel>

    <bean id="upperService" class="sipackage.service.UppercaseService"></bean>

    <int:service-activator requires-reply="false" input-channel="inChannel" ref="upperService" method="toUpper"></int:service-activator>
</beans>

JUnit

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/META-INF/spring/integration/sample.xml"})
public class ChannelTest {

    @Autowired MessageChannel inChannel;

    @Test
    public void test() {

        boolean sendOutcome=inChannel.send(MessageBuilder.withPayload("Hello, there 1!").build());
        assertTrue(sendOutcome);

        sendOutcome=inChannel.send(MessageBuilder.withPayload("Hello, there 2!").build());
        assertTrue(sendOutcome);
    }

}

服务

public class UppercaseService {

public String toUpper(String msg)
{
    return msg.toUpperCase();
}
}
4

3 回答 3

10

根据“配置服务激活器”

当服务方法返回非空值时,端点将尝试将回复消息发送到适当的回复通道。要确定回复通道,它将首先检查端点配置中是否提供了“输出通道”......如果没有可用的“输出通道”,它将检查消息的回复通道标头值。

它没有提到的是,任何产生回复的消息处理程序的基本行为是,如果它没有通过这两个检查找到任何东西,它就会抛出一个异常,这可以在sendReplyMessage() 的方法中看到AbstractReplyProducingMessageHandler,许多此类事物共享的基类。因此,如果您有一个非 void 服务方法,您必须在消息上设置一个 output-channel 或一个 replyChannel 标头。

SI 人员建议的一个选项是在您的服务激活器前面放置一个 header-enricher,它将replyChannel 标头设置为“nullChannel”。因为默认情况下不会覆盖标头,所以任何现有的 replyChannel 都将按预期工作,其他所有内容都将转储到 nullChannel。

至于 requires-reply 属性,它用于处理一个完全不同的问题,即您有一个可能会生成null而不是有效消息的组件。该标志允许您指示null应将响应转换为异常。您可以在“消息传递网关错误处理”的注释和“没有响应到达时的网关行为”中找到对此的讨论。

于 2013-01-25T04:20:29.457 回答
7

requires-reply="false"意思是“没有定义返回的方法可以void返回null”。

如果该方法确实返回了回复,我们需要某个地方来发送它。如前所述guido- 如果您想忽略结果,请将其设置output-channel为 nullChannel。

于 2013-01-25T04:17:36.667 回答
0

属性:需要-回复

指定服务方法是否必须返回非空值。此值默认为“false”,但如果设置为“true”,则当底层服务方法(或表达式)返回空值时,将引发 ReplyRequiredException。

于 2015-06-16T16:05:56.307 回答