[英]What is good way to register adjacent HTTP requests with the Spring integration flow
在Spring集成流中注冊相鄰HTTP請求的好方法是什么?
我的應用程序是:
對於每個客戶(都有自己的流程,該流程由輪詢者安排)
...
操作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.