我在 for 循环中触发出站网关。 迭代在日期范围内,我的目的是用当前迭代中的日期动态更改 uri 的参数。
我的迭代:
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
}
}
这里是触发http调用的方法:
public void produceHttpCall(LocalDate param) {
MessageChannel gateway = context.getBean("gatewayChannel", DirectChannel.class);
gateway.send(new GenericMessage<>("")); // should i pass in some way as payload ?
}
出站网关组件:
@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;
}
我正在读一些关于出站网关的内容,它说组件在上下文初始化期间建立了一次连接,但我不想相信它不存在为每次迭代更改日期 uri 参数的方法。 感谢您的帮助
所以,您的 HTTP 出站网关的 URI 可能有一个变量,类似于
https://foo.host/bars/{date}
。然后你添加:
handler.setUriVariableExpressions(Map.of("date", new FunctionExpression<Message<?>>(m -> m.getPayload())));
并将您的
date
作为您发送给gatewayChannel
的消息的有效负载传递。