Java Articles

Advertisement

Spring Integration Header Enricher Example

Share

There are times when you want to add some additional information to the original payload.  You may want to enhance the request, add/modify its contents or just the header part.

For the case of content part, think of a case where we know the customer information like the id, name etc. but we don’t have customer’s billing info. Before we send the message further down the route, we may want to enrich the customer info with the billing info. The Content enricher component is responsible for this.

For the case of header part, it can be as simple as generating a sequence number and stashing it in the header properties.

Spring provides Header as well as Payload Enricher. In this article, we will look into the header enricher.

Header Enricher Example

Advertisement
  1. Set a static value <header name="justValue" value="x" />
  2. Set bean as the header <header name="itsBean" ref="headerBean" />
  3. Set header using spring defined elements. Setting priority below. <priority value="1"/>
  4. Set header to a bean’s value. <header name="beanValue" ref="headerBean" method="value" />
  5. Set header to a bean’s value which works on an existing payload property. <header name="beanEnrichValue" ref="headerBean" method="enrichValue" /> Method which enriches the value: public String enrichValue(SomePayload payload) { return "'" + payload.getId() + "' enriched"; }
  6. Set header to a java object. <header name="dateObjectUsingExpr" expression="new java.util.Date()"/>
  7. Set header using concatenation of a payload method and the system time. <header name="serialNbrFetchedFromPayloadUsingExpr" expression="payload.uniqueNbr() + '_' + T(java.lang.System).currentTimeMillis()" />

Let’s combine all of the above ways into our integration configuration. We will use spring provided element <header-enricher> to enhance the headers.

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:beans="http://www.springframework.org/schema/beans">

<channel id="inputChannel" />
<channel id="outputChannel" />

<header-enricher input-channel="inputChannel"
output-channel="outputChannel">
<header name="justValue" value="x" />
<header name="itsBean" ref="headerBean" />
<priority value="1" />
<header name="beanValue" ref="headerBean" method="value" />
<header name="beanEnrichValue" ref="headerBean" method="enrichValue" />
<header name="dateObjectUsingExpr" expression="new java.util.Date()" />
<header name="serialNbrFetchedFromPayloadUsingExpr"
expression="payload.uniqueNbr() + '_' + T(java.lang.System).currentTimeMillis()" />
</header-enricher>

<beans:bean id="headerBean"
class="com.javarticles.spring.integration.enricher.HeaderBean" />
</beans:beans>
Header Enricher
Header Enricher

HeaderBean:

Advertisement
package com.javarticles.spring.integration.enricher;

import java.io.Serializable;

public class HeaderBean implements Serializable {
private static final long serialVersionUID = 1L;

public String value() {
return "value from headerBean";
}

public String enrichValue(SomePayload payload) {
return "'" + payload.getId() + "' enriched";
}

public String toString() {
return "I am HeaderBean";
}
}

SomePayload:

package com.javarticles.spring.integration.enricher;

import java.util.concurrent.atomic.AtomicLong;

public class SomePayload {
private String id;
private AtomicLong serialNbr = new AtomicLong(0);

public SomePayload(String id) {
this.id = id;
}

public String toString() {
return "Payload: " + id;
}

public String getId() {
return id;
}

public long uniqueNbr() {
return serialNbr.incrementAndGet();
}

}

We first load the context. Get the input channel bean. Subscribe a message handler to the output channel so we know once the message is posted to the output channel.
In the message handler, we print all the header properties.
Finally, we initiate the process by sending a message to the input channel.

SpringIntegrationHeaderEnricherExample:

package com.javarticles.spring.integration.enricher;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.SubscribableChannel;

public class SpringIntegrationHeaderEnricherExample {

public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"headerEnricherApplicationContext.xml");
try {
MessageChannel inputChannel = (MessageChannel) context.getBean("inputChannel");
SubscribableChannel outputChannel = (SubscribableChannel) context.getBean("outputChannel");
outputChannel.subscribe(new MessageHandler() {

public void handleMessage(Message<?> message) throws MessagingException {
MessageHeaders headers = message.getHeaders();
for (String key : headers.keySet()) {
System.out.println("Key: " + key + " , value: " + headers.get(key));
}
}
});
inputChannel.send(MessageBuilder.withPayload(new SomePayload("Header Enricher")).build());
} finally {
context.close();
}
}

}

Output:

Key: serialNbrFetchedFromPayloadUsingExpr , value: 1_1430631037410
Key: justValue , value: x
Key: beanValue , value: value from headerBean
Key: beanEnrichValue , value: 'Header Enricher' enriched
Key: dateObjectUsingExpr , value: Sun May 03 11:00:37 IST 2015
Key: id , value: b1281044-1c70-7b33-81b4-cec65ad40e3c
Key: itsBean , value: I am HeaderBean
Key: priority , value: 1
Key: timestamp , value: 1430631037411

Download the source code

This was an example about spring integration header enricher. You can download the source code here: springintegrationEnricher.zip

Advertisement

Share

Related

Advertisement

Latest

Advertisement