繁体   English   中英

如何将 object 从 spring 集成中的流传递到下一个流?

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

在这里,我有 3 个不同的流程,我正在使用 spring 集成 dsl。 假设我们在流程 1 中准备了一个 object,我想将 object 传递给其他流程,而不会干扰来自网关的实际有效负载。 所以我只想在某处以某种方式添加 object 但不更改实际有效负载,以便我可以在后续流程中使用该 object。 我可以在 header 中传递它,但是在 header 中发送大 object 是否正确?

在这里,我使用具有三个并行流的分散聚集模式。

     @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

或者让我们假设在 validateRequest 之后我想创建一个 object 并希望以某种方式将其传递给并行流/someFlow,但我不想妨碍将要进入流的有效负载。 通过使用 header 是可以实现的,但是有没有其他方法可以实现呢?

我们可能需要查看您的流拓扑和有关您的解决方案的更多详细信息,但是在消息头中传输大对象确实没有问题。

您也许还可以研究发布-订阅或收件人列表解决方案,以将相同的消息发送到不同的子流。 这样,您可以在这些子流中对请求进行任何您需要的操作,而无需查看标头。

更新

因此,根据您对 scatter-gather 的评论,听起来您想向收件人分发消息,收集他们的结果,然后继续发送原始请求消息。

既然您说您不希望向这些收件人发送额外的 header,但仍保留请求消息以进行收集后处理,那么您可以研究一下enrich enrich() EI 模式,其中您的 scatter-gather 将是请求子流。 在那里,您可以决定将收集结果添加到 header 或请求有效负载的属性中(如果可能的话)。

在文档中查看有关内容丰富器的更多信息: https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#payload-enricher

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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