我是 Spring/Spring Integration 的新手,无法理解如何正确实现允许可变数量的出站网关调用的流程。
问题:给定客户 ID,我进行一次出站网关调用以获取相关客户的列表。对于每个相关客户,我都需要进行另一个出站网关调用。
我的初始方法:第一次服务调用后,我用收到的相关客户 ID 填充队列,调用辅助方法来轮询队列,然后通过调用出站继续流程再次网关执行下一个请求(仅当有客户离开时)。收到响应后,网关会调用相同的辅助方法来重复该过程。
如果队列为空,则使用 header 将流路由到辅助方法内的 logAndReply(请参阅下面的代码)。
private IntegrationFlow helperMethod()
{
return flow -> flow
.transform(payload -> {
currentCust = info.poll();
return payload;
})
//check if there is another related-customer to process
.handle(
(payload, headers) -> MessageBuilder
.withPayload(payload)
.copyHeaders(headers)
.setHeader("relatedCustomerLeft", (null == currentCust ? Boolean.FALSE : Boolean.TRUE))
.build()
)
.route(("headers['relatedCustomerLeft']"), route -> route
//if no more related-customers, end the flow
.subFlowMapping(Boolean.FALSE,
subFlow -> subFlow
.logAndReply(LoggingHandler.Level.INFO, "end of base case")
)
//if there is a related customer, call the gateway
.subFlowMapping(Boolean.TRUE,
subGateway()
)
);
}
这里,subGateway() 只是返回另一个处理出站服务调用的 IntegrationFlow。当我删除递归调用时,这个逻辑似乎有效。但是,包含递归调用会导致启动时出现堆栈溢出错误。日志只是一遍又一遍地显示“Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol”,直到发生堆栈溢出。
"Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getFlow' defined in file [GetFlow.class]: Initialization of bean failed; nested exception is java.lang.StackOverflowError"
我的问题是
- 为什么会发生堆栈溢出?
- 递归是否可能按照我尝试使用的方式进行?如何正确实现?
- 是否有更好的方法来进行可变数量的网关调用?有什么例子吗?
正如我所说,我还是个新手,所以任何提示或资源也将不胜感激。
最佳答案
首先,您没有展示如何创建该递归,其次不清楚为什么在您的用例中需要递归。
听起来您需要一个enrich()
“来获取相关客户的列表”。
然后,您需要针对返回的客户列表进行 split() 来为每个客户调用服务。对我来说似乎就是这样!不需要任何递归。
我还会考虑在 split()
之前使用 filter()
以确保列表不为空。或者 route()
正如你已经...
但仍然不清楚递归会在哪里弹出......
关于java - Spring 集成: Recursive Flow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59938820/