Spring Integration Content Enricher Example

0

Spring provides Header as well as Payload Enricher. We enrich a content when there is a missing information and is available from some external data source. In my last article, I showed you header enrich example. In this article, I will show you content enrich example.

Content Enrich Example

We have a customer ID and we want to get all the customer information for the given ID. The customer information contains id, billing address and credit points.

Imagine, the billing information like the billing address and the credit points are maintained by some billing service.

We will first get whatever customer information we could from the customer service. We will then make use of a content enricher to delegate the call to the billing service to get the billing information. Once we have the billing information, we will add the missing details.

Here is the customer service.

CustomerService:

package com.javarticles.spring.integration.enricher;

public interface CustomerService {
    Customer findCustomerById(String id);
}

Customer bean.

Customer:

package com.javarticles.spring.integration.enricher;

public class Customer {
    private String id;
    private String billingAddress;
    private int creditPoints;

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

    public String getId() {
        return id;
    }

    public void setBillingAddress(String billingAddress) {
        this.billingAddress = billingAddress;
    }

    public String getBillingAddress() {
        return billingAddress;
    }
        
    public int getCreditPoints() {
        return creditPoints;
    }

    public void setCreditPoints(int creditPoints) {
        this.creditPoints = creditPoints;
    }

    public String toString() {
        return "Customer: " + id + ", address: " + billingAddress;
    }

}

Implementation of customer service.

CustomerServiceImpl:

package com.javarticles.spring.integration.enricher;

public class CustomerServiceImpl {

    public Customer findCustomerById(String id) {
        return new Customer(id);
    }

}

The missing billing details.

BillingInfo:

package com.javarticles.spring.integration.enricher;

public class BillingInfo {
    private Customer customer;
    private String address;
    private int creditPoints;
    
    public BillingInfo(Customer customer, String address, int creditPoints) {
        this.customer = customer;
        this.address = address;
        this.creditPoints = creditPoints;
    }
    public Customer getCustomer() {
        return customer;
    }
    public String getAddress() {
        return address;
    }
    public int getCreditPoints() {
        return creditPoints;
    }
}

Billing Service will return us the BillingInfo for the Customer passed in.

BillingService:

package com.javarticles.spring.integration.enricher;

public interface BillingService {
    BillingInfo getBillingAddress(Customer customer);
}

BillingServiceImpl:

package com.javarticles.spring.integration.enricher;

public class BillingServiceImpl {

    public BillingInfo getBillingAddress(Customer customer) {
        if (customer.getId().equals("Joe")) {
            return new BillingInfo(customer, "Joe Billing Address - XYZ", 22);
        } else if (customer.getId().equals("Sam")) {
            return new BillingInfo(customer, "Sam Billing Address - ABC", 1001);
        }
        return new BillingInfo(customer, customer.getId() + " - " + "UNKNOWN", 0);
    }
    
}

Let’s configure our integration flow in the application context XML.

We have a Gateway component to service customer details requests. The default-request-channel is set to custChannel. Service activator findCustomer is subscribed to custChannel. The Customer object returned is passed on to the output channel custOutputChannel. Our content enricher defined by tag <enricher> is listening on input-channel custOutputChannel. Moment the customer object enters the channel, it is delegated to the request-channel billChannel to get the billing details. Service Activator findBillingAddressServiceActivator is subscribed to billChannel and returns us the billing information.

Using the <property> child element within the <enricher> element we set the missing information.

	<enricher id="enricher" input-channel="custOutputChannel"
		request-channel="billChannel">
		<property name="billingAddress" expression="payload.address" />
		<property name="creditPoints" expression="payload.creditPoints" />
	</enricher>

Here is the complete configuration.

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/task http://www.springframework.org/schema/task/spring-task.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" xmlns:task="http://www.springframework.org/schema/task">

	<channel id="custChannel" />	
	<channel id="custOutputChannel" />
	<channel id="billChannel" />

	<gateway id="custGateway" default-request-timeout="5000"
		default-reply-timeout="5000"
		service-interface="com.javarticles.spring.integration.enricher.CustomerService"
		default-request-channel="custChannel">
	</gateway>

	<enricher id="enricher" input-channel="custOutputChannel"
		request-channel="billChannel">
		<property name="billingAddress" expression="payload.address" />
		<property name="creditPoints" expression="payload.creditPoints" />
	</enricher>

	<service-activator id="findCustomer"
		ref="customerService" method="findCustomerById" input-channel="custChannel"
		output-channel="custOutputChannel" />

	<beans:bean id="customerService"
		class="com.javarticles.spring.integration.enricher.CustomerServiceImpl" />

	<service-activator id="findBillingAddressServiceActivator"
		ref="billingService" method="getBillingAddress" input-channel="billChannel"
		/>

	<beans:bean id="billingService"
		class="com.javarticles.spring.integration.enricher.BillingServiceImpl" />
</beans:beans>
Content Enrich Flow

Content Enrich Flow

In our main class, we get the customer gateway bean and then call on findCustomerById to get the complete customer details.

SpringIntegrationPayloadEnricherExample:

package com.javarticles.spring.integration.enricher;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringIntegrationPayloadEnricherExample {

    public static void main(String[] args) throws InterruptedException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            CustomerService customerService = (CustomerService) context.getBean("custGateway");
            System.out.println(customerService.findCustomerById("Joe"));
            System.out.println(customerService.findCustomerById("Sam"));
        } finally {
            context.close();
        }
    }
    
}

Output:

Customer: Joe, address: Joe Billing Address - XYZ
Customer: Sam, address: Sam Billing Address - ABC

Download the source code

This was an example about spring integration content enrich. You can download the source code here: springintegrationPayloadEnricher.zip

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at rsatish.m@gmail.com

Comments are closed.