在出站网关 HttpRequestExecutingMessageHandler 中,动态参数 URI 的更改。

huangapple go评论76阅读模式
英文:

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(&quot;gatewayChannel&quot;, DirectChannel.class);
    gateway.send(new GenericMessage&lt;&gt;(&quot;&quot;)); // should i pass it in some way as payload  ?
}

The Outbound Gateway component:

@ServiceActivator(inputChannel = &quot;gatewayChannel&quot;)
@Bean
public HttpRequestExecutingMessageHandler outBoundGatewayCall(@Value(&quot;${ws.uri}&quot;) 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(&quot;date&quot;, new FunctionExpression&lt;Message&lt;?&gt;&gt;(m -&gt; m.getPayload())));

And pass your date as a payload of that message you send to the gatewayChannel.

huangapple
  • 本文由 发表于 2023年4月19日 22:03:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定