简体   繁体   English

在 Spring 集成中从收集器返回的响应不一致

[英]Inconsistent response coming back from gatherer in Spring integration

I'm using Scatter-gather pattern and calling 3 sub-flows parallelly.我正在使用 Scatter-gather 模式并并行调用 3 个子流。 Then gathering them and aggregating them.然后收集它们并聚合它们。 I'm trying to get the result back after aggregating.我试图在聚合后取回结果。 I'm getting the response as List but the two responses that are there in the list are not consistent.我得到的响应是列表,但列表中的两个响应不一致。 When I'm trying to get by doing .get(0), sometime it's giving me 1st services's response other time 2ns service's response.当我试图通过执行 .get(0) 来获得时,有时它会给我第一个服务的响应,而其他时间是 2ns 服务的响应。 It's inconsistent.这是不一致的。 Kindly suggest me a way to understand the output so that I can assign it to the actual variable.请建议我一种理解输出的方法,以便我可以将其分配给实际变量。

Config file配置文件

 @Configuration
    public class IntegrationConfiguration {
      @Autowired LoansServiceImpl loansService;
    
      long dbId = new SequenceGenerator().nextId();
  //   Main flow
  @Bean
  public IntegrationFlow flow() {
    return flow ->
        flow.split()
            .log()
            .channel(c -> c.executor(Executors.newCachedThreadPool()))
            .convert(LoanProvisionRequest.class)
            .scatterGather(
                scatterer ->
                    scatterer
                        .applySequence(true)
                        .recipientFlow(flow1())
                        .recipientFlow(flow2())
                        .recipientFlow(flow3()),
                gatherer -> gatherer.releaseLockBeforeSend(true))
            .log()
            .aggregate(a -> a.outputProcessor(MessageGroup::getMessages))
            .channel("output-flow");
  }
  //   flow1
  @Bean
  public IntegrationFlow flow1() {
    return integrationFlowDefination ->
        integrationFlowDefination
            .channel(c -> c.executor(Executors.newCachedThreadPool()))
            .handle(
                message -> {
                  try {
                    lionService.saveLionRequest(
                        (LionRequest) message.getPayload(), String.valueOf(dbId));
                  } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                  }
                });
  }

  //   flow2
  @Bean
  public IntegrationFlow flow2() {
    return integrationFlowDefination ->
        integrationFlowDefination
            .channel(c -> c.executor(Executors.newCachedThreadPool()))
            .handle(
                message ->
                    lionService.getData(
                        (LionRequest) message.getPayload(), SourceSystem.PROVISION))
            .log();
  }

  //  flow3
  @Bean
  public IntegrationFlow flow3() {
    return integrationFlowDefination ->
        integrationFlowDefination
            .channel(c -> c.executor(Executors.newCachedThreadPool()))
            .handle(
                message ->
                    lionService.prepareCDRequest(
                        (LionRequest) message));
  }

  @Bean
  public MessageChannel replyChannel() {
    return MessageChannels.executor("output-flow", outputExecutor()).get();
  }

  @Bean
  public ThreadPoolTaskExecutor outputExecutor() {
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    pool.setCorePoolSize(4);
    pool.setMaxPoolSize(4);
    return pool;
  }
}

Gateway网关

@MessagingGateway
public interface LionGateway {

  @Gateway(requestChannel = "flow.input", replyChannel = "output-flow")
  List<?> echo(LionRequest lionRequest);
}

Controller控制器

@Autowired private LionGateway lionGateway;

 @PostMapping(value = "/invoke-integration")
  public String invokeIntegrationFlow(@RequestBody LionRequest lionRequest) {
    String response1 = lionGateway.echo(lionRequest).get(0);
    String response2 = lionGateway.echo(lionRequest).get(1);
    System.out.Println("response2 ")
    System.out.Println("response1 ")

    return "response";
  }

in the Controller I have two variables ie, response1 & response2 these two values are coming as inconsistent.在控制器中,我有两个变量,即 response1 和 response2 这两个值不一致。 Help me with a better approach.用更好的方法帮助我。

Since you are using executor channels, the order is simply the order in which the replies come back.由于您使用的是执行程序通道,因此顺序就是回复返回的顺序。 The gatherer knows nothing about the downstream topology.收集器对下游拓扑一无所知。

You will need to deal with the random order of the reply list in your code.您将需要处理代码中回复列表的随机顺序。

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

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