Home > Software engineering >  Spring Integration: can one call a gateway from within a header enricher?
Spring Integration: can one call a gateway from within a header enricher?

Time:06-17

I need to make a number of calls to gateways and to then place the return payloads into headers. These gateway calls are really sending a message to a channel behind which there is a chain. It currently looks like this:

<int:channel id            = "call_api_a" />
<int:chain   input-channel = "call_api_a" >
    ...
</int:chain>

<int:channel id            = "call_api_b" />
<int:chain   input-channel = "call_api_b" >
    ...
</int:chain>
<int:gateway request-channel="call_api_a" />

<int:header-enricher>
    <int:header name="a" expression="payload" />
</int:header-enricher>

<int:gateway request-channel="call_api_b" />

<int:header-enricher>
    <int:header name="b" expression="payload" />
</int:header-enricher>

Is there an elegant way to embed the call to the gateway inside of the header-enricher?

Something like:

<int:header-enricher>
    <int:header name="a">
        <int:gateway request-channel="call_api_a" />
    </int:header>
</int:header-enricher>

I know that the above doesn't work but gives the sense of what I'd like to achieve.

Thanks for any pointers and best regards.

-aljo

CodePudding user response:

Not <gateway>, but like this:

<int:header-enricher>
    <int:header name="a"  ref="myGateway" method="gatewayMethod"/>
</int:header-enricher>

The <gateway> is bean based on some service interface. You just point in the <header> definition to that bean and its method to be called against your payload or message.

There is some explanation in the docs: https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher

CodePudding user response:

It looks like the "additional gateway definition" is the way to go on this one. Thanks, @artem-bilan, for pointing me in the right direction.

I want to call to my chain-channel directly from a header-enricher. If my chain-channel is:

<int:channel id            = "call_api_a" />
<int:chain   input-channel = "call_api_a" >
    ...
</int:chain>

Define somewhere in the Java add your gateway interface.

package com.app.wonderful;

@FunctionalInterface
public interface MyChainChannelGatewayMethod {
    Message<?> callChainChannel(Message<?> message) throws MessageException; 
}

Define a gateway that points to your chain-channel:

<int:gateway default-request-channel = "call_api_a"
             id                      = "call_api_a_GW"
             service-interface.      = "com.app.wonderful.MyChainChannelGatewayMethod" />

Then you can call your gateway in-line from the SpEL expression in the header-enricher:

<int:header-enricher >
    <int:header name="a"  expression="@call_api_a_GW(#root).payload" />
</int:header>

Does the trick for me. Thanks again.

  • Related