Spring 集成配置现有流程

Spring Integration configure existing flow

如果我们有这样的事情:

private void createFlow(String pathOfEndpoint) {

        IntegrationFlow integrationFlow = IntegrationFlows
                                                    .from(confGateway(pathOfEndpoint))
                                                    .<Map<String,String>>handle((p, h) -> {
                                                        return doSomething(p);
                                                    })
                                                    .get();

        getIntegrationFlowContext().registration(integrationFlow).id(pathOfEndpoint).register();
    }

而 confGateway 是:

HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
...
return gateway;

所以我们创建了流程并进行了注册。我的问题是,有没有办法在运行时配置更多的流,比如一段时间后添加另一个 .handle((p,h) -> .... 或者如果需要添加 .transform(...)。我尝试使用 integrationFlow.configure(....) 但我在 StandardIntegrationFlow 中看到该方法是这样的:

@Override
    public void configure(IntegrationFlowDefinition<?> flow) {
        throw new UnsupportedOperationException();
    }

是否可以稍后以某种方式添加另一个 .handle(...) 或类似的内容?如果没有,我可以通过其他方式使用现有流程进行操作吗?也许子流? 我要感谢 Artem Bilan 对 Spring 集成的大力支持和示例。继续努力。

没有;您不能在运行时修改流程;但是,您可以使用 dynamic and runtime integration flows 并删除流程并用新流程替换它。

不清楚为什么会在运行时创建一个 IntegrationFlow 然后决定稍后修改它...不过没关系。您需要自己了解 IntegrationFlow 只是一个逻辑配置组件。在消息处理期间,它在运行时不执行任何操作。我想说的是,在运行时我们有一堆活动组件,它们对 IntegrationFlow 创建它们一无所知。它们已经使用 EIP 原则相互绑定 - 通道、端点、轮询器等。

当然,这些组件很灵活,可以在运行时进行调整,但绝对不是 IntegrationFlow 创建的。我的意思是您可以 subscribeunsubscribe into/from channels 在运行时使用他们的合同。或者您可以将轮询端点添加(结束删除 - stop() 它)到现有 QueueChannel 中。所有其他组件不会受到影响。 松耦合原则是EIP解决方案的要点之一。

所以,你问的关于动态 .handle().transform() 更多的是关于订阅新消费者到现有渠道。由于自己很难做到这一点(你需要对 Spring 基础知识稍深),你仍然可以使用提到的 IntegrationFlowContext.registration())。您需要的是原始流中的通道名称,并使用该通道(或其名称)构建具有所需 .handle().transform() 逻辑的新 IntegrationFlow

IntegrationFlow integrationFlow = 
                    IntegrationFlows
                             .from(confGateway(pathOfEndpoint))
                             .<Map<String,String>>handle((p, h) -> {
                                            return doSomething(p);
                             })
                             .channel("mainChannel")
                             .get();

 ... 

 IntegrationFlow otherFlow = 
                    IntegrationFlows
                             .from("mainChannel")
                             .transform(...)
                             .get();