Dynamically instantiating Spring Integration flows

我的梦境 提交于 2019-12-23 20:23:25

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/38493071/dynamically-instantiating-spring-integration-flows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!