Scatter Gather分布模式下correlation-strategy-expression配置报错求助
使用Scatter Gather模式的分布式变体时,在gatherer元素中通过Header配置correlation-strategy-expression后,出现报错:No reply produced by handler xxxx and its 'requireReply' property is set to true。
配置代码如下:
<scatter-gather input-channel="inputDistribution" output-channel="output" gather-channel="gatherChannel"> <scatterer> <recipient channel="distribution1Channel"/> <recipient channel="distribution2Channel"/> <recipient channel="distribution3Channel"/> </scatterer> <gatherer correlation-strategy-expression="headers[CorrelationId]'/> </scatter-gather>
测试代码中已显式设置Header:
MessageBuilder.withPayload("foo").setHeader(MessageHeaders.REPLY_CHANNEL,output).setHeader("CorrelationId","foo").build()
注:在scatterer中设置apply-sequence=true时,组件可正常运行。
1. 表达式语法错误
你的gatherer配置里,correlation-strategy-expression的语法存在错误:末尾多了一个单引号,且Header名称的引用格式不正确。正确的表达式写法有两种:
<!-- 方式1:使用单引号包裹Header名称 --> <gatherer correlation-strategy-expression="headers['CorrelationId']"/> <!-- 方式2:使用点语法 --> <gatherer correlation-strategy-expression="headers.CorrelationId"/>
语法错误会导致Spring无法正确解析关联策略,gatherer无法识别消息的关联标识,进而无法收集到匹配的回复消息,最终触发requireReply报错。
2. 分布式场景的消息关联要求
在分布式Scatter Gather场景中,仅在请求消息中设置CorrelationId还不够,必须确保所有分支处理后的回复消息都完整保留这个CorrelationId头。如果分支的处理逻辑中丢失了该Header,gatherer无法将回复与原始请求关联,会一直等待回复直至触发异常。
3. apply-sequence=true的作用解析
当开启apply-sequence=true时,Spring Integration会自动为消息添加sequenceNumber、sequenceSize和correlationId等标准关联头,自动完成消息的分组关联,因此无需手动配置关联策略也能正常运行。但手动配置关联策略时,必须保证语法正确且所有回复消息的关联标识一致。
是否需要额外Bean?
不需要额外定义任何Bean,只要修正关联策略表达式的语法错误,同时确保所有分支回复都保留CorrelationId头即可解决问题。
内容的提问来源于stack exchange,提问作者nileshb

