簡體   English   中英

在Spring集成流中注冊相鄰HTTP請求的好方法是什么

[英]What is good way to register adjacent HTTP requests with the Spring integration flow

在Spring集成流中注冊相鄰HTTP請求的好方法是什么?

我的應用程序是:

對於每個客戶(都有自己的流程,該流程由輪詢者安排)

  • 源應用程序中的GET操作1,結果為JSON_1
  • 將JSON_1 POST到遠程系統,結果為JSON_1B
  • 將JSON_1B POST到源應用程序,結果為JSON_1C
  • 源應用程序中的GET操作2,結果為JSON_2
  • 將JSON_2 POST到遠程系統,結果是JSON_2B
  • 將JSON_2B POST到源應用程序,結果為JSON_2C

...

  • 源應用程序中的GET操作n,結果為JSON_N
  • 將JSON_N POST到遠程系統,結果為JSON_NB
  • 將JSON_NB POST到源應用程序,結果為JSON_NC

操作1、2,...,n必須按順序

這是我的示例程序(為簡單起見,所有REST調用均針對同一類)

@Configuration
public class ApplicationConfiguration {

@Autowired
private IntegrationFlowContext flowContext;

@Bean
public MethodInvokingMessageSource integerMessageSource() {
    final MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setObject(new AtomicInteger());
    source.setMethodName("getAndIncrement");
    return source;
}

@PostConstruct
public void createAndRegisterFlows() {
    IntegrationFlowBuilder integrationFlowBuilder = createFlowBuilder();
    for (int i = 1; i <= 2; i++) {
        integrationFlowBuilder = flowBuilder(integrationFlowBuilder, i);
    }
    integrationFlowBuilder.handle(CharacterStreamWritingMessageHandler.stdout());
    flowContext.registration(integrationFlowBuilder.get()).register();
}

private IntegrationFlowBuilder createFlowBuilder() {
    return IntegrationFlows.from(this.integerMessageSource(), c -> c.poller(Pollers.fixedRate(5000)));
}

private IntegrationFlowBuilder flowBuilder(final IntegrationFlowBuilder integrationFlowBuilder, final int number) {
    return integrationFlowBuilder
            .handle(Http.outboundGateway("http://localhost:8055/greeting" + number).httpMethod(HttpMethod.GET)
                    .expectedResponseType(String.class))
            .channel("getReceive" + number)
            .handle(Http.outboundGateway("http://localhost:8055/greeting" + number).httpMethod(HttpMethod.POST)
                    .expectedResponseType(String.class))
            .channel("postReceive" + number)
            .handle(Http.outboundGateway("http://localhost:8055/greeting-final" + number)
                    .httpMethod(HttpMethod.POST).expectedResponseType(String.class))
            .channel("postReceiveFinal" + number);
}
}

該程序運行集成流程

GET http://localhost:8055/greeting1
POST http://localhost:8055/greeting1 (previous result as an input)
POST http://localhost:8055/greeting-final1 (previous result as an input)
GET http://localhost:8055/greeting2
POST http://localhost:8055/greeting2 (previous result as an input)
POST http://localhost:8055/greeting-final2 (previous result as an input)

這正在按預期方式工作。 但我想知道這樣做的好方法,因為在調用GET http://localhost:8055/greeting2未使用來自調用POST http://localhost:8055/greeting-final1的響應。 我只希望這些呼叫按此順序進行。

實際上,您擁有該循環所需的一切,可以創建對REST服務的子流調用。 只有你所缺少的是一個payload從結果greeting-final1這是會隨着消息的公布.channel("postReceiveFinal" + number) 第二次迭代將把greeting2"訂閱到該通道,並且payload可在此處進行處理。不確定如何重做flowBuilder()方法,但是您只需要使用消息中的payload來滿足您的任何需求,例如,可以將其用作:

/**
 * Specify an {@link Expression} to evaluate a value for the uri template variable.
 * @param variable the uri template variable.
 * @param expression the expression to evaluate value for te uri template variable.
 * @return the current Spec.
 * @see AbstractHttpRequestExecutingMessageHandler#setUriVariableExpressions(Map)
 * @see ValueExpression
 * @see org.springframework.expression.common.LiteralExpression
 */
public S uriVariable(String variable, Expression expression) {

將有效負載注入某些請求參數,因為它只是HttpMethod.GET

handle(Http.outboundGateway("http://localhost:8055/greeting2?param1={param1}")
        .httpMethod(HttpMethod.GET)
        .uriVariable("param1", "payload")
        .expectedResponseType(String.class))

暫無
暫無

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

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