About Nitin Kumar

Nitin Kumar works as a Solution Architecture in Wipro Technologies specialize in SOA. He has over twelve years of experience in information technology architecting and developing software systems, focus mainly on JEE solutions.

Spring Integration: A lightweight integration Approach

Today’s application expects access of all business in enterprise environment regardless of application technologies, which require, seamless integration with desperate systems. This integration can be achieved through wiring of desperate systems using Middleware technologies.

Integration platform enables environment for applications to share information with each other to make architecture high interoperable. Spring Integration provides a mediation framework to build lightweight integration solution using routing and mediation, regardless of protocols. The Spring Integration is lightweight routing and mediation framework and doesn’t need heavy weight ESB container to deploy.

Spring Integration uses Message object to communicate, route and pass the data, which is nothing, but a wrapper on Java object consists of payload and header. Payload consist data which could be any type such as File, String, stream etc. and header consist generic information on message such as id, timestamp etc.

Message communicates to producer via channel, which decoupled source from destination and post the message to any protocols such as JMS, HTTP, Ldap, file etc. Producers send Messages to a channel, and consumers receive Messages from a channel

spring-integration_simple

Simple Integration Application

Below example show how producer send an employee object to channel and publisher receive message from channel.

Spring Dependency Maven configuration

For starting simple integration example we just need to add the core spring integration and spring context dependency to maven pom.xml. Moreover we also need Junit and spring test to enable unit testing

<properties>
 <spring.framework.version>3.2.3.RELEASE</spring.framework.version>
 <spring.integration.version>2.2.4.RELEASE</spring.integration.version>
 <junit.version>4.11</junit.version>
 </properties>
 <dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.integration</groupId>
 <artifactId>spring-integration-core</artifactId>
 <version>${spring.integration.version}</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>${junit.version}</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>${spring.framework.version}</version>
 <scope>test</scope>
 </dependency>
 </dependencies>

Spring Configuration
We have to configure channel and gateway in spring configuration to send and receive message

SpringIntegTest-context.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"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.2.xsd">
 <!--
 sendRequest
 receiveRequest
 -->
 <annotation-config/>
 <context:component-scan base-package="org.springsample.integration"/>
 <gateway id="request" service-interface="org.springsample.integration.SentRequest"/>
 <channel id="sendRequest"/>
 <outbound-channel-adapter channel="sendRequest" ref="receiveResponse" method="processMessage" />
</beans:beans>

Here we created request gateway to send the message to channel and outbound adapter to receive message from the sendRequest channel. Gateway of Spring Integration is entry points to send messages, which specify using Java Interface. Spring Integration automatically define proxy implementation at runtime

Request and Receive

Below we create the SentRequest and ReceiveResponse class to send and receive message as follows

SentRequest.java

package org.springsample.integration;
import org.springframework.integration.annotation.Gateway;
public interface SentRequest {
 @Gateway(requestChannel="sendRequest")
 public void process(Employee emp);

}

@Gateway annotation will indicate entry point to send message

package org.springsample.integration;
import org.springframework.integration.Message;
import org.springframework.integration.annotation.MessageEndpoint;
@MessageEndpoint
public class ReceiveResponse {
public void processMessage(Message<Employee> message) {
 Employee employee = message.getPayload();
 System.out.println("Message Received \n Name :"+employee.getName()+"/n Phone : "+employee.getPhone()+"/n Address :"+employee.getAddress());

 }
}

@MessageEndpoint will indicate that it will receive message from channel via adapter.

Not but not leas below is Employee POJO

package org.springsample.integration;
public class Employee {
 private String name;
 private String title;
 private String address;
 private String phone;

public Employee(String name, String phone, String address) {this.name=name;this.phone=phone; this.address=address;
//……..Getter amd Setter
 }
}

Testing

We can test this using spring test framework as below

package org.springbyexample.integration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringIntegTest {
 @Autowired
 private SentRequest request = null;
 @Test
 public void testIntegration() {
 Employee emp = new Employee("John", "12345678", "Sunny Street Mac RG1");
 request.process(emp);
 }
}

Make sure you keep spring config file name SpringIntegTest-context in org/springbyexample/integration and should be in your class path
While running SpringIntegTest, it will display console message as follows

Message Received
Name :John/n Phone : 12345678/n Address :Sunny Street Mac RG1

Summary

Spring Integration is open source simple integration that enhances loose coupling and make application integration easy and simpler. It will integrate, route and mediate the message across the channel and gateway in configurable way. This article helps to understand about Spring Integration and will aid you to develop a simple integration application.

Resources

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Spring Integration: A lightweight integration Approach"

  1. Eric Shiau says:

    Please update source code download link. Link is broken.

Leave a Reply


× 7 = twenty eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close