gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:22 25 4
gpt4 key购买 nike

为什么我在指定了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com