2013-01-25 35 views
4

为什么我收到我指定requires-reply="false"Spring集成:服务激活需要回复=“假”的使用

异常

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(); 
} 
} 
+0

您的意思是客户对下游服务的返回值不感兴趣?使用output-channel =“nullChannel”; require-reply应该用于返回void或null的方法afaik –

+0

谢谢,但我知道您提供的两种解决方案。我认为require-reply标志是为了忽略返回的值。如果不是这个标志的目的可能是什么? –

+0

默认情况下require-reply为false。将下游组件的requires-reply属性设置为“true”,以确保只要下游组件在内部返回null,就立即引发抛出异常的响应。 –

回答

8

作为每"Configuring Service Activator"

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

什么也没有提到有任何回复产生信息处理的基本行为是,如果它没有找到与这两个检查什么,它抛出一个异常,如可在the sendReplyMessage() method可见AbstractReplyProducingMessageHandler是一个由许多此类事物共享的基类。因此,如果你有一个无效的服务方法,你必须在你的消息上设置一个输出通道或者一个replyChannel头。

其中一个选项suggested by the SI guys是在您的服务激活器前放置一个header-richher,它将replyChannel标头设置为“nullChannel”。由于标题默认不会被覆盖,任何现有的replyChannel都将按预期工作,其他所有内容都将转储到nullChannel。

至于requires-reply属性,这是为了处理一个完全不同的问题,其中有一个组件可能会生成null而不是有效的消息。该标志允许您指出null响应应该变成异常。您可以在"Messaging Gateway Error Handling""Gateway behavior when no response arrives"的注释中找到相关讨论。

5

requires-reply="false"的意思是 “没关系,这不是定义的方法返回void回报null”。

如果方法返回一个答复,我们需要在某个地方发送它。正如guido所说 - 如果您想忽略结果,请将output-channel设置为nullChannel。

0

属性:需要回复

指定服务方法是否必须返回一个非空值。 默认情况下,此值为'false',但如果设置为'true',则当基础 服务方法(或表达式)返回空值时,将会抛出ReplyRequiredException。

相关问题