通过Spring集成流程注册相邻HTTP请求的好方法是什么

时间:2018-10-01 09:52:09

标签: spring spring-integration spring-integration-dsl

在Spring集成流中注册相邻HTTP请求的好方法是什么?

我的申请是:

对于每个客户(都有自己的流程,该流程由轮询者安排)

  • 在源应用程序中获取操作1,结果为JSON_1
  • 将JSON_1发布到远程系统,结果为JSON_1B
  • 将JSON_1B发布到源应用程序,结果为JSON_1C
  • 在源应用程序中执行操作2,结果为JSON_2
  • 将JSON_2发布到远程系统,结果为JSON_2B
  • 将JSON_2B发布到源应用程序,结果是JSON_2C

...

  • 源应用程序中的GET操作n,结果为JSON_N
  • 将JSON_N发布到远程系统,结果为JSON_NB
  • 将JSON_NB发布到源应用程序,结果为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)

这按预期工作。但是我想知道这样做的好方法,因为调用POST http://localhost:8055/greeting-final1中没有使用来自调用GET http://localhost:8055/greeting2的响应。我只希望这些呼叫按此顺序进行。

1 个答案:

答案 0 :(得分:0)

实际上,您拥有该循环所需的一切,可以创建对REST服务的子流调用。 import numpy as np values = [1,2,3,4,5,6,7,8,9,10,11] cond = [df['tla']<85000, (df['tla'] >= 850000) & (df['tla'] < 110000), .... ] df['bid_bucket'] = np.select(cond, values) 只会导致您丢失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