Dynamically instantiating Spring Integration flows

2019-07-07 07:30发布

I have a Spring Integration Flow Project that exposes a Rest Gateway, after receiving the Rest POST request, it does some minor logic. Based on some Payload parameters I would like to Activate another Spring Integration flow dynamically and route the message to a designated channel in that flow that I can discover in the Primary flow based on the Payload. The Sub flow will put the response message in a designated channel that is defined in the primary flow.

How can I achieve this.

2条回答
Emotional °昔
2楼-- · 2019-07-07 08:08

Starting with version 1.2 Spring Integration Java DSL provides an API for runtime flow registration:

@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);

So, according to your logic you can build and perform one flow or another.

Around that API you can even build some cache do not register the same flow several times, but just reuse after the first registration.

查看更多
手持菜刀,她持情操
3楼-- · 2019-07-07 08:17

See the dynamic ftp example which uses a custom router to instantiate the sub flow and route to the new flow's channel.

Also see my answer to this question and its follow up for a similar mechanism, this time using java configuration for inbound mail adapters.

查看更多
登录 后发表回答