Skip to content

Enriching Headers Using Gateways

Gary Russell edited this page Jul 7, 2014 · 5 revisions

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>
  1. This expression passes the current message (#root) to the the statusFlow gateway. Since there is no service-interface attribute on the gateway, a RequestReplyExchanger is used, with the method exchange which receives and returns a Message<?>.

  2. The gateway forwards the message to the getStatusForDeal channel which is the input channel to a <chain/>. There, the payload is transformed to a field dealId which is then used to perform a database lookup.

  3. 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.

  4. Finally, the 'STATUS' column value is retrieved from the result and the 'status' header is set to this value.

  5. The message sent to channel output has the original payload and a header status containing the value.