首页 > 解决方案 > Spring Integration - 重放相同的流程

问题描述

我有一个像

@Bean
public IntegrationFlow processRequest() {
    return flow -> flow.channel(REQUEST_INPUT)
            .transform(...)
            .enrichHeaders(h -> h.<Storage>headerFunction(...)))
            .enrich(this::...)

            //.route(ifReplayIsNeeded(), routeToSameFlow())

            .enrich(this::...)
            .route(..., ...)
            .route(..., ...)
            .enrich(this::...)
            .handle(...)

            //.route(ifReplayWasNeeded(), routeBack())

            .route(..., ...)
            .enrich(this::...)
            .transform(...)
            .channel(REQUEST_OUTPUT);
}

因此,当满足条件时(请参阅 参考资料ifReplayIsNeeded()processRequest(),必须再次调用流程。然而,不是必须执行整个流程,而是几乎在最后(-> ifReplayWasNeeded())这个内部流程必须回到它被调用的地方并完全处理原始流程。

routeToSameFlow()外观(用于Storage存储请求/响应和流中使用的其他数据)

Consumer<RouterSpec<Boolean, MethodInvokingRouter>> routeToSameFlow() {
    return rs -> rs.resolutionRequired(false)
            .subFlowMapping(true, sf -> sf
                    // 1. storing the current Storage
                    .enrichHeaders(h -> h.<Storage>headerFunction("store", s -> s))
                    // 2. creating the req for the internal flow
                    .transform(internalRequestMapper, "mapFrom")
                    // 3. routing to the beginning of the flow
                    .route(Message.class, (m) -> REQUEST_INPUT)
                    // 4. defining the channel where the internal flow will return to
                    .channel("RETURN_CHANNEL")
            )
            .defaultOutputToParentFlow();
}

routeBack()

Consumer<RouterSpec<Boolean, MethodInvokingRouter>> routeBack() {
    return rs -> rs.resolutionRequired(false)
            .subFlowMapping(true, sf ->
                    sf.route(Message.class, (m) -> "RETURN_CHANNEL")
            )
            .defaultOutputToParentFlow();
}

当我收到以下错误时,我肯定错过了一些概念:

原因:org.springframework.beans.factory.BeanCreationException:“currentComponent”(org.springframework.integration.router.MethodInvokingRouter@60a0f09f)是单向“MessageHandler”,不适合配置“outputChannel”。这是集成流程的结束。

你能帮我如何实现这样的逻辑吗?我应该将主要流程拆分为较小的 IntegrationFlow 吗?
我想尽可能少地干扰主流,这就是为什么我只想在开头添加一条导流路线,在结尾添加一条返回路线。有可能这样做吗?

谢谢!
问候,
V。

标签: spring-integrationspring-integration-dsl

解决方案


您可能使所有这些路由器的逻辑过于复杂。考虑在您的主要流程中间只使用简单channel()的定义,并作为您想要返回的子流程的输出。Java DSL 没有任何限制,只是channel()在开头和结尾都有定义:您可以自由添加channel()流定义的任何点。例如:

 .handle(...)
 .channel("middleOfTheFlow")
 .route(ifReplayWasNeeded(), routeBack())

然后你的routeBack()流定义可能就.channel("middleOfTheFlow")在最后,在这里处理后的消息将被传递到middleOfTheFlow主流的通道。

换句话说,anIntegrationFlow只是将某些业务功能的端点保持在同一位置的逻辑组件,但这并不意味着流中的所有端点都非常严格地相互关联。您始终可以在两者之间有一个通道,并从其他地方向该通道发送消息,订阅该通道的端点将处理该消息。

在文档中查看更多信息:https ://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl


推荐阅读