Spring Integration:链内用Recipient List Router实现迭代遇路由错误
解决Spring Integration链内迭代循环的配置问题
问题核心
你遇到的错误本质是:Recipient List Router在未匹配到任何接收通道时,若未配置默认输出通道,无法处理消息。链的输出通道不会自动成为路由器的默认通道,必须显式指定。
可行的声明式配置方案
方案一:给路由器配置默认输出通道
修改链B的Recipient List Router,直接将消息头中的replyChannel设为默认输出通道:
<int:chain input-channel="B_channel" > <int:gateway request-channel="C_channel" /> <int:transform ... set the payload to indicate if still more work to do ... /> <int:recipient-list-router resolution-required="false" default-output-channel="#{headers.replyChannel}"> <int:recipient channel="B_channel" select-expression="payload.size() != 0" /> </int:recipient-list-router> </int:chain>
当payload.size() == 0(即无需继续循环)时,路由器会自动将消息发送到链A网关指定的replyChannel,完成循环并返回结果。
方案二:添加匹配终止条件的接收者
通过覆盖所有条件的接收者配置,避免路由器出现无通道可匹配的情况:
<int:chain input-channel="B_channel" > <int:gateway request-channel="C_channel" /> <int:transform ... set the payload to indicate if still more work to do ... /> <int:recipient-list-router resolution-required="true"> <int:recipient channel="B_channel" select-expression="payload.size() != 0" /> <int:recipient channel="#{headers.replyChannel}" select-expression="payload.size() == 0" /> </int:recipient-list-router> </int:chain>
这种方式无需设置resolution-required=false,因为两个接收者的条件覆盖了所有可能情况,路由器总能找到对应的输出通道,彻底避免报错。
关键注意事项
- 链A通过网关调用链B时,Spring Integration会自动在消息中注入
replyChannel头,指向网关的回复通道,因此#{headers.replyChannel}的引用是安全有效的。 - 请根据实际业务逻辑,将
payload.size() !=0替换为你真实的循环终止判断表达式。
内容的提问来源于stack exchange,提问作者al.truisme
相关产品推荐
相关产品推荐

