【问题标题】:spring integration: service activator requires-reply="false" usagespring 集成:服务激活器 requires-reply="false" 用法
【发布时间】:2013-01-08 22:57:03
【问题描述】:

为什么即使在我指定了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();
}
}

【问题讨论】:

  • 你的意思是客户端对下游服务的返回值不感兴趣?使用 output-channel="nullChannel"; requires-reply 应该用于返回 void 或 null afaik 的方法
  • 谢谢,但我知道您提出的两种解决方案。我认为 requires-reply 标志是为了忽略返回的值。如果不是,这个标志的目的可能是什么?
  • requires-reply 默认为 false。将下游组件的 requires-reply 属性设置为“true”,以确保在下游组件内部返回 null 后立即通过抛出异常产生及时响应。

标签: java spring spring-integration


【解决方案1】:

属性:需要-回复

指定服务方法是否必须返回非空值。 该值默认为“false”,但如果设置为“true”,则 底层的时候会抛出ReplyRequiredException 服务方法(或表达式)返回一个空值。

【讨论】:

    【解决方案2】:

    根据"Configuring Service Activator"

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

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 2017-01-03
        • 2019-01-15
        • 2017-01-30
        • 2012-07-22
        相关资源
        最近更新 更多