java - Spring 集成 : service activator requires-reply ="false" usage

标签 java spring spring-integration

为什么我在指定了 requires-reply="false"

后仍收到以下异常

异常

org.springframework.integration.support.channel.ChannelResolutionException:没有可用的输出 channel 或 replyChannel header

配置

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

最佳答案

根据 "Configuring Service Activator" :

when the service method returns a non-null value, the endpoint will attempt to send the reply message to an appropriate reply channel. To determine the reply channel, it will first check if an "output-channel" was provided in the endpoint configuration... If no "output-channel" is available, it will then check the Message's replyChannel header value.

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

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

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

关于java - Spring 集成 : service activator requires-reply ="false" usage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514003/

相关文章:

java - 如何获取 DOM 对象中两个元素之间的文本?

java - Spring 集成 AbstractReplyProducingMessageHandler doInit 与 onInit

java - 如何在服务器停机后恢复 Spring Batch 作业

Spring MVC 使用表单 :checkbox to bind data

java - 无法使用 spring 5 Autowiring hibernate 5 sessionfactory

java - spring tcp 套接字,授权客户端并处理未决响应

java - 调用静态方法并将模型传递给 Controller

java - Spring REST WS : jersey vs resteasy vs reSTLet vs apache cxf vs Spring WS

java - 将 JButton 放在右上角

java - 有没有一种方法可以链接 mysql 中已有的条目而不是创建新条目?