簡體   English   中英

如何在 Spring 集成中添加可配置的超時

[英]How to add configurable timeout in spring integration

我在我的項目中使用 Spring-integration java dsl,我想為正在調用的並行服務添加可配置的超時。 我應該能夠為我在這里使用的不同流添加不同的超時。 請幫我解決這個問題。

下面是代碼庫:

配置文件

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

網關

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

控制器

@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";
  }

目前尚不清楚該超時應該是什么以及在哪里。 但是,如果您對我們的推測感到滿意,這里是其中一種選擇:

.recipientFlow(f1 -> f1.gateway(flow1(), gateway -> gateway.replyTimeout(2000L)))

replyTimeout也可以是非常動態的並且基於請求消息:只需分別設置一個receiveTimeout標頭!

否則,請詳細說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM