Search code examples
springspring-integration

Spring Integration: can one call a gateway from within a header enricher?


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


Solution

  • 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