简体   繁体   中英

How do I pass an object to the next flow from a flow in spring integration?

Here I have 3 different flows and I'm using spring integration dsl. Let's assume we have prepared an object in flow 1 and I want to pass that object to other flows without disturbing the actual payload that's coming from the gateway. So I just want to add the object somehow in somewhere but not changing the actual payload so that I can use that object in subsequent flows. I can pass that in header but will that be correct to send a big object in header?

Here I'm using scatter gather pattern with three parallel flows.

     @Bean
      public IntegrationFlow flow() {
                return flow ->
                    flow.handle(validatorService, "validateRequest")
                        .split()
                        .channel(c -> c.executor(Executors.newCachedThreadPool()))
                        .scatterGather(
                            scatterer ->
                                scatterer
                                    .applySequence(true)
                                    .recipientFlow(flow1())
                                    .recipientFlow(flow2())
                                    .recipientFlow(flow3()),
                            gatherer ->
                                gatherer
                                    .releaseLockBeforeSend(true)
                                    .releaseStrategy(group -> group.size() == 2))
                        .aggregate(lionService.someMethod())
        // here I want to call other Integration flows
                        .gateway(someFlow())
                        .to(someFlow2());
  
            }

//Here in this flow I'm calling prepareCDRequestFromLionRequest method in the handle(). This method returns an object1 which is one of the payload(among 3) that will be used after aggregation but I want to prepare another object2 in this method and somehow want to send that object2 to the someFlow() or someFlow2() but I want object1 as a payload. 
    
     @Bean
      public IntegrationFlow flow1() {
        return flow ->
            flow.channel(c -> c.executor(Executors.newCachedThreadPool()))
                .enrichHeaders(h -> h.errorChannel("flow1ErrorChannel", true))
                .handle(cdRequestService, "prepareCDRequestFromLionRequest");
      }
    //same way I have flow2 and flow3

Or let's suppose after validateRequest I want to create an object and want to pass that to the parallel flows/someFlow somehow but I don't want to hamper the payload that will be coming to the flows. By using header it's achievable but is there a different way to achieve this?

We probably need to see your flows topology and more details about your solution, but there is indeed no problem to transfer big objects in the message headers.

You perhaps may also look into a publish-subscribe or recipient-list solution to send the same message to different sub-flows. This way you can do in those sub-flows whatever you need with request without looking into headers.

UPDATE

So, according your comment about scatter-gather, it sounds like you want to distribute a message to the recipients, gather their results and then continue together with an original, request message.

Since you say that you'd like do not send an extra header to those recipients, but still preserve a request message for post-gather processing, then you may look into a enrich() EI pattern, where your scatter-gather would be a request sub-flow. There you can decide to add a gather result into a header or property of the request payload (if that is possible).

See more info about Content Enricher in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#payload-enricher

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM