【问题标题】:Spring Integration DSL: How to refactor into Subflows?Spring Integration DSL:如何重构为子流?
【发布时间】:2021-07-15 20:38:54
【问题描述】:

为了使我的 Spring Integration DSL 代码更具可读性和模块化,我想将像 .scatterGather() 这样的复杂操作提取到子流中。

以此为例说明重构前代码的外观:

@Bean
IntegrationFlow testFlow() {
        return IntegrationFlows
                .from(Http.inboundChannelAdapter("test").get())
                .scatterGather(
                        s -> s
                                .applySequence(true)
                                .recipientFlow(getSubFlow2()),
                        g -> g.outputProcessor(MessageGroup::getOne))
                .log(INFO, m -> m)
                .routeToRecipients(route -> route
                        .recipientFlow(IntegrationFlowDefinition::nullChannel)
                        .get())
                .get();
}

private IntegrationFlow getSubFlow2() {
    return f -> f.handle((m, h) -> 42);
}

这是我想提取到它自己的子流程和方法中的部分:

                .scatterGather(
                        s -> s
                                .applySequence(true)
                                .recipientFlow(getSubFlow2()),
                        g -> g.outputProcessor(MessageGroup::getOne))
                .log(INFO, m -> m)

我想象结果看起来像这样:

@Bean
IntegrationFlow testFlow() {
        return IntegrationFlows
                .from(Http.inboundChannelAdapter("test").get())
                .someMethod(getSubFlow1())
                .routeToRecipients(route -> route
                        .recipientFlow(IntegrationFlowDefinition::nullChannel)
                        .get())
                .get();
}

private IntegrationFlow getSubFlow1() {
        return f -> f
                .scatterGather(
                        s -> s
                                .applySequence(true)
                                .recipientFlow(getSubFlow2()),
                        g -> g.outputProcessor(MessageGroup::getOne))
                .logAndReply(INFO, m -> m)
}

private IntegrationFlow getSubFlow2() {
    return f -> f.handle((m, h) -> 42);
}

这可以通过 Spring Integration DSL 以某种方式完成吗?怎么样?

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    gateway(IntegrationFlow)流定义方法:

        /**
    
     * Populate the "artificial"
    
     * {@link org.springframework.integration.gateway.GatewayMessageHandler} for the
    
     * provided {@code subflow}.
    
     * Typically used with a Java 8 Lambda expression:
    
     * <pre class="code">
    
     * {@code
    
     *  .gateway(f -> f.transform("From Gateway SubFlow: "::concat))
    
     * }
    
     * </pre>
    
     * @param flow the {@link IntegrationFlow} to to send a request message and wait for reply.
    
     * @return the current {@link BaseIntegrationFlowDefinition}.
    
     */
    
    public B gateway(IntegrationFlow flow) {
    

    【讨论】:

    • 太棒了。看起来.gateway() 正是我想要的。将 .gateway() 包含在 docs.spring.io/spring-integration/reference/html/… 下的动词/端点列表中是否有意义?我错误地假设该列表是详尽无遗的。
    • 另外,我在 spring-integration-samples 中找不到.gateway() 的示例,我无法在网上找到很多关于使用此端点的 Spring Integration DSL 示例。
    • 是的,我们可以做到。我认为它甚至应该在该 DSL 中拥有自己的部分。请提出 GH 问题,我们会尽快解决。我不认为这样的原始人值得整个独立样本
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2018-03-24
    相关资源
    最近更新 更多