-
Couldn't load subscription status.
- Fork 1.1k
Enriching Headers Using Gateways
This technique can be used to supply values for headers from sub-flows, much like an enricher can enhance a payload.
Note: This is now obsolete and only applies to version 2.2; since 3.0, the <enricher/> element can now set headers as well as payload properties.
Consider the following:
<int:header-enricher input-channel="input" output-channel="output">
<int:header name="foo" expression="@fooGetter.calculateFoo(payload)" />
</int:header-enricher>
<bean id="fooGetter" class="foo.FooGetter" />
The expression invokes the calculateFoo() method, passing in the payload, and the result becomes the value of header 'foo'.
Now, if all we need to do is use some field of the payload to perform a database lookup, using the above technique, we’d have to write some Java code, perhaps invoking a JdbcTemplate to do the lookup.
However, it can be done entirely with configuration…
<int:header-enricher input-channel="input" output-channel="output"> (5) <int:header name="status" expression="@statusFlow.exchange(#root).payload['STATUS']" /> (1) (4) </int:header-enricher> <int:gateway id="statusFlow" default-request-channel="getStatusForDeal" /> (2) <int:chain input-channel="getStatusForDeal"> <int:transformer expression="payload.dealId" /> <jdbc:outbound-gateway query="select status from trade_details where dealId = :payload" data-source="dataSource" /> (3) </int:chain>
-
This expression passes the current message (
#root) to the thestatusFlowgateway. Since there is noservice-interfaceattribute on the gateway, aRequestReplyExchangeris used, with the methodexchangewhich receives and returns aMessage<?>. -
The gateway forwards the message to the
getStatusForDealchannel which is the input channel to a<chain/>. There, the payload is transformed to a fielddealIdwhich is then used to perform a database lookup. -
Since the chain has no output-channel, the result (a message with a payload that is a map of columns/values) is returned to the gateway, which returns it as the result of the partial expression.
-
Finally, the 'STATUS' column value is retrieved from the result and the 'status' header is set to this value.
-
The message sent to channel
outputhas the original payload and a headerstatuscontaining the value.