动态实例化 Spring Integration 流程

Use*_*273 4 spring-integration

我有一个 Spring Integration Flow 项目,它公开了一个 Rest 网关,在收到 Rest POST 请求后,它会执行一些小逻辑。基于一些有效载荷参数,我想动态激活另一个 Spring Integration 流并将消息路由到该流中的指定通道,我可以根据有效载荷在主流中发现该通道。子流会将响应消息放在主流中定义的指定通道中。

我怎样才能做到这一点。

Art*_*lan 5

1.2Spring Integration Java DSL版本开始,提供了一个用于运行时流注册的 API :

@Autowired
private IntegrationFlowContext context;
...

IntegrationFlow myFlow = f -> f
            .<String, String>transform(String::toUpperCase)
            .transform("Hello, "::concat);

String flowId = this.context.register(myFlow);
MessagingTemplate messagingTemplate = this.context.messagingTemplateFor(flowId);

assertEquals("Hello, SPRING",
            messagingTemplate.convertSendAndReceive("spring", String.class));

this.context.remove(flowId);
Run Code Online (Sandbox Code Playgroud)

因此,根据您的逻辑,您可以构建和执行一个或另一个流程。

你甚至可以围绕那个 API 构建一些缓存,不要多次注册相同的流,而只是在第一次注册后重用。

  • 以防万一它可以帮助其他人,完全注册您需要使用的流程: this.context.registration(flow).register() (2认同)