java spring 集成:服务激活器 requires-reply="false" 用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14514003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:30:35  来源:igfitidea点击:

spring integration: service activator requires-reply="false" usage

javaspringspring-integration

提问by Aravind Yarram

Why am I receiving the below exception even after I've specified requires-reply="false"

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

Exception

例外

org.springframework.integration.support.channel.ChannelResolutionException: no output-channel or replyChannel header available

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

Config

配置

<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

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

}

Service

服务

public class UppercaseService {

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

回答by Ryan Stewart

As per "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.

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

What it doesn't mention there is that the basic behavior of any reply-producing message handler is that if it doesn't find anything with those two checks, it throws an exception, as can be seen in the sendReplyMessage() methodof the AbstractReplyProducingMessageHandler, a base class shared by many such things. Thus if you have a non-void service method, you either have to set an output-channel or a replyChannel header on your messages.

什么它没有提及有任何回复产生信息处理的基本行为是,如果它没有找到与这两个检查什么,它抛出一个异常,如中可以看到的sendReplyMessage()方法的AbstractReplyProducingMessageHandler,一个被很多这样的东西共享的基类。因此,如果您有一个非空的服务方法,您必须在您的消息上设置一个 output-channel 或一个 replyChannel 标头。

One option suggested by the SI guysis to put a header-enricher in front of your service activator that will set the replyChannel header to "nullChannel". Because headers aren't overwritten by default, any existing replyChannel will work as intended, and everything else will be dumped to the nullChannel.

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

As for the requires-reply attribute, that's for handling an entirely different problem where you have a component that might produce nullinstead of a valid messages. That flag allows you to indicate that a nullresponse should be turned into an exception. You'll find a discussion of this in the note on "Messaging Gateway Error Handling"and in "Gateway behavior when no response arrives".

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

回答by Gary Russell

requires-reply="false"means "it's OK that a method that is not defined to return voidreturns null".

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

IF the method DOES return a reply, we need someplace to send it. As guidosaid - if you want to ignore the result, set the output-channelto nullChannel.

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

回答by Flavio Troia

Attribute: requires-reply

属性:需要回复

Specify whether the service method must return a non-null value. This value will be 'false' by default, but if set to 'true', a ReplyRequiredException will be thrown when the underlying service method (or expression) returns a null value.

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