spring integration:service activator requires-reply ="false"用法

Ara*_*ram 5 java spring spring-integration

为什么即使在我指定之后我也收到了以下异常 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>
Run Code Online (Sandbox Code Playgroud)

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

服务

public class UppercaseService {

public String toUpper(String msg)
{
    return msg.toUpperCase();
}
}
Run Code Online (Sandbox Code Playgroud)

Rya*_*art 9

根据"配置服务激活器":

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

什么它没有提及有任何回复产生信息处理的基本行为是,如果它没有找到与这两个检查什么,它抛出一个异常,如中可以看到的sendReplyMessage()方法的AbstractReplyProducingMessageHandler,是许多此类事物共享的基类.因此,如果您使用非void服务方法,则必须在消息上设置输出通道或replyChannel标头.

SI人员建议的一个选项是在服务激活器前放置一个标题扩展器,将replyChannel标头设置为"nullChannel".由于默认情况下不会覆盖标头,因此任何现有的replyChannel都将按预期工作,其他所有内容都将转储到nullChannel.

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


Gar*_*ell 6

requires-reply="false"表示"没有定义返回的方法void返回null"是可以的.

如果该方法返回一个回复,我们需要一个地方发送它.如上所述guido- 如果要忽略结果,请将其设置output-channel为nullChannel.