英文:
Dinamically param uri change in outbound gateway HttpRequestExecutingMessageHandler
问题
I'm triggering an outbound gateway inside a for cycle.
The iteration is in a range of dates and my purpose is to dynamically change the URI's param with the date in the current iteration.
My iteration:
void startIteration(LocalDate from, LocalDate to) {
for (LocalDate date = from; date.isBefore(to); date = date.plusDays(1)) {
produceHttpCall(date); // date is the param I want to change in each HTTP call
}
}
Here is the method triggering the HTTP call:
public void produceHttpCall(LocalDate param) {
MessageChannel gateway = context.getBean("gatewayChannel", DirectChannel.class);
gateway.send(new GenericMessage<>("")); // Should I pass it in some way as a payload?
}
The Outbound Gateway component:
@ServiceActivator(inputChannel = "gatewayChannel")
@Bean
public HttpRequestExecutingMessageHandler outBoundGatewayCall(@Value("${ws.uri}") String uri) {
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(uri + **date param**);
handler.setHttpMethod(HttpMethod.GET);
handler.setExpectedResponseType(String.class);
handler.setOutputChannel(myOutputChannel());
return handler;
}
I'm reading something about the Outbound Gateway that says the component is establishing the connection once during context initialization, but I don't want to believe that it does not exist a way to change the date URI param for each iteration.
Thanks for your help.
英文:
I'm triggering an outbound gateway inside a for cycle.
The iteration is in a range of dates and my purpose is to dinamically change the uri's param with the date in the current iteration.
My iteration:
void startIteration(LocalDate from, LocalDate to) {
for (LocalDate date = from; date.isBefore(to); date = date.plusDays(1)) {
produceHttpCall(date); // date is the param i want to change in each http call
}
}
Here is the method triggering the http call:
public void produceHttpCall(LocalDate param) {
MessageChannel gateway = context.getBean("gatewayChannel", DirectChannel.class);
gateway.send(new GenericMessage<>("")); // should i pass it in some way as payload ?
}
The Outbound Gateway component:
@ServiceActivator(inputChannel = "gatewayChannel")
@Bean
public HttpRequestExecutingMessageHandler outBoundGatewayCall(@Value("${ws.uri}") String uri) {
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(uri + **date param**);
handler.setHttpMethod(HttpMethod.GET);
handler.setExpectedResponseType(String.class);
handler.setOutputChannel(myOutputChannel());
return handler;
}
I'm reading something about Outbound Gateway that says the component is establishing the connection once during context initialization but i don't want to believe that it does not exist a way to change the date uri param for each iteration.
Thanks for your help
答案1
得分: 1
请看文档:https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#mapping-uri-variables。
因此,HTTP出站网关的URI可能具有一个变量,类似于https://foo.host/bars/{date}
。然后,您添加:
handler.setUriVariableExpressions(Map.of("date", new FunctionExpression<Message<?>>(m -> m.getPayload())));
并将您的date
作为消息的负载传递给gatewayChannel
。
英文:
See documentation: https://docs.spring.io/spring-integration/docs/current/reference/html/http.html#mapping-uri-variables.
So, your URI for that HTTP Outbound Gateway may have a variable, something like https://foo.host/bars/{date}
. Then you add:
handler.setUriVariableExpressions(Map.of("date", new FunctionExpression<Message<?>>(m -> m.getPayload())));
And pass your date
as a payload of that message you send to the gatewayChannel
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论