CarbonRider - Articles

ESB or Spring Integration

Posted by Carbon Rider on Sunday, September 5th, 2010

With every new language and new frameworks, systems developed today are getting extremely complex to understand. Buzz words like cross cutting concerns, separation of concerns and distributed applications are just adding complexity to the problems. Over past few years, development of large scale applications required development of various subsystems and integrating those systems to achieve common organisational goal.
It is always noticed that big organisations carry out their daily business operations on various systems developed and deployed on heterogeneous platforms and technology. Integration is required not for just, the in-house applications but geographically distributed business requires interfacing with subsystems spread across the globe. While it is not always possible to replace legacy systems, organisations are always looking to build new systems that can interface with old systems and carry out business process.
The problem of making systems communicate with each other is always crucial for growth and to carry out business activities smoothly. Well, that is where Enterprise Integration plays vital role.
A need was felt to have common language that can address enterprise level problem and help to built better software. Enterprise Integration patterns were the step towards building that common vocabulary. The patterns address most of the issues faced while building communication link between various systems.
Though Enterprise Integration patterns provide theoretical solution to the enterprise level problems, there was no readymade software available to address all above concerns.

Enterprise Service Bus
To address the Integration issues, a strong need was felt to have software that can help systems to communicate with each other and abstract the underlying protocol required for communication. Enterprise Service Bus was invented to address this concern and gained popularity within less time. There are many vendors providing their own ESB implementation helping business to integrate the systems.
Though ESB seems to be perfect choice for implementing Enterprise Integration, it has certain drawbacks like efforts required to implement a solution, most of the ESB use XML as data format for communication – require additional overhead to process the message, high hardware configuration requirements etc. On top of all, what If I am interested only in few Integration mechanisms and don’t want to use all nice to have features of ESB. In simple terms, can I get only features I am interested in and not whole ESB?

Any other option?
Well, over last few years’ communities like Mule Soft has been successful in implementing ESB that provide numerous features by keeping the complexity low.
In Jan 2008, there was one more project announced by a popular community in Java World – SpringSource. The aim of this project was to use similar techniques used by the leading Java AOP Framework – Spring and extending it further in Enterprise world. The framework is known as Spring Integration.

SPRING INTEGRATION
Integrating systems could be a complex task and it could be even more complex when it is developed and deployed on heterogeneous platforms. In enterprise world, system integration is typically done via messaging, file uploads, webservices etc. But using any of the means soon tend to become complex and require too much of development effort and understanding of how the connectors actually work. Developers need to understand the Integration design and should have capability to build a model which can be easily customized as per the requirements.
Spring integration framework is built to simplify the Enterprise integration with minimal coding and high flexibility. With the help of Spring Integration framework one can develop components that adheres to separation of concern principle and develop integration code with low or no coupling.
Let’s consider a scenario wherein two systems need to exchange a data via messaging. Following are the brief details about the scenario
1. System “A” does some processing and puts messages in Message Queue “MyQueue” (Message Queue is created on Apache Active MQ Server.)
2. Whenever a message is received in Queue (MyQueue) it should be transmitted to Queue “queue/C” (Message Queue is created on JBoss MQ server.)
In above scenario, using J2EE one can write a Message Driven Bean and receive messages stored on Queue “MyQueue”. The messages can be later transferred to destination Queue “queue/C” by using JNDI object and messaging API. Though this sounds simple, it would certainly require some effort from development side to write Java Code.
With the help of Spring Integration, there is no need to write a single line of Java code to develop above functionality. All of the above functionality can be achieved merely by writing few lines of XML. Just have a look at the next code (Don’t worry about what every line does.)




	
            

	
             

	
		

	
	

	
             

	
             	

	
		
			
		
	

	
	

	

That’s all you require to achieve the above functionality. Set of XML tags and you are done. (Did I just say that you don’t need to write any java code, well if you want to see the above configuration running, you should write a bootstrap class which will load this configuration file OR you can deploy this as web application and load the above configuration file through context loader provided by SPRING.)

To run the sample, you should have following softwares
1. SPRING integration
2. SPRING 2.5 or above
3. Active MQ (You should create a queue as “sourceQueue”)
4. JBoss (In this example we will use default queue – queue/C)

The point I want to bring here is not about how I achieved this task by just using XML but the level of simplicity provided by Spring Integration framework. Though this is very simple use case, the configuration can be further extended to meet real-time project requirements.

WHAT NEXT
Well the example that I just explained is very simple, let’s consider following scenarios

S#

Scenario

Detailed Requirement

1

Scenario1

ABC Ltd has very huge customer base and maintains customer portal. Customers can register on portal and view premium payment, maintain profile information etc. ABC Ltd wants to add a new feature to its system, so that registered users can retrieve payment details for a given policy without a need of login to Portal.
A solution was provided that customers can send an email to portal server and mention policy number in subject line. A utility has to be developed on server to poll messages after specific interval and reply to valid emails after considering “From” address and policy number.

2

Scenario 2

XYZ Ltd requires currency details to manage its business across continents. The data should be refreshed after every one hour.
PQR Ltd provides currency information in flat file format. The file is available over FTP. To update currency information in system, XYZ Ltd has developed a Webservice.
A utility has to be developed which will poll FTP server, parse information and invoke webservice after a specific interval.

 

 

 

Let’s evaluate above scenarios
1. To achieve functionality described in scenario 1, developer would require writing a component which checks if there is new email available in the mailbox. Developer need not have to worry about the scheduling activity as it is the built in feature available in Spring Integration framework (poller).
The additional functionality of retrieving policy details and sending new email with details can be coded in different components leading to loosely coupled design.
The component responsible for sending email with policy details would simply act on a logical channel. Also the component performing authorisation need not have to be aware of email sending component. Its primary responsibility is to just perform authentication of the given data (In this case it would be policy no and sender email address.)
2. For scenario 2, a component similar to what explained in scenario 1 can be implemented and by making use of poller feature, the new feed can be checked after specific interval. As and when the feed data is available the component has to just return value object holding data, the Spring integration framework will transmit data to a channel. This activity will lead to execution of one more component which would receive data as an input and can invoke webservice. Remember you can trigger multiple actions as and when any data is transferred to a channel and it can be easily configured using XML.

Let’s have a look at the following diagrams to understand the flow of operations for example 1.

Spring Integration Email Example

The diagram indicates that the two components “Authentication” & “Email Sender” are invoked as soon as the data is available on the channels they are associated with. Note that the association between the components and channel is done through configuration and there is no hardcoded reference within components. (In this example any Java class capable of performing application specific operations are considered as component.)
Poller pings Email server at specific intervals to check if any email has arrived. If email contents matches to the criteria, required data is posted on channel1. Since Channel 1 is configured as input channel for Authentication component, the Authentication component will begin its execution. After the processing is complete, data returned by Authentication component can then be transferred to channel 2. There is no need to call any API from Authentication component to transfer data to channel 2. On receipt of data on channel 2 will lead to execution of Email Sender component. Following diagram shows logical flow of execution.

Spring Integration Email Example Components

Components would be invoked only when there is appropriate message is available on channel. With patterns like “Transformer” and spring features like dependency injection, components would be never aware of the environment under which they are executed and would mostly work with POJO objects (Spring Integration framework can make use of all the Core-Spring framework features).
One of the advantage using Spring integration would be the solution can be built in quick time, without a need of application server, no need of high configuration hardware. Also the configuration is pretty simple and less painful. Developer doesn’t have to learn anything specific to application server or go through the process of wizard based configuration.
Let’s compare Spring integration with JEE to find out how it could be still a better choice.
To begin with, consider following set of questions and evaluate what both frameworks offer

S#   JEE Spring Integration

1

What built-in scheduling mechanism?

JEE doesn’t offer any scheduling mechanism. Though some may argue that using java.util.Timer one can simulate scheduling activities, it would require hours of efforts on developer part.

Comes with built-in poller feature.

2

Messaging based component execution?

JEE does offer JMS, through which one can write message listener bean and process certain business logic. The major drawback of JMS is that it is tied up with only Topic, Queue type of messaging and doesn’t offer any solution for other channels. (FTP, email, webservice etc)

The channel concept used in Spring for information interchange is not tied up with underlying messaging system, rather the channels are logical entities and they do not exist physically.

3

SOA or Enterprise Integration pattern support

No answer. JEE mostly talks about different technology that it offers and doesn’t offer any out of box solution to SOA or Enterprise patterns.

Spring integration implements various SOA or Enterprise Integration patterns.

4

Cost

Commercially available Application servers are costly and require heavy investment. One may also choose open source / freeware implementation.

Sponsored by Spring and hence available as open source, freeware.

5

Feasibility

While one may be interested only in messaging and webserver functionality of Application Server, but still has pay for entire suite.

Spring projects are available as modules and hence can be configured as per requirement, Spring Integration is developed as one of the Spring module.

6

Interoperability

While JEE components are based on JSR standards, it’s not always possible to migrate applications as is from one Application Server to another. Due to the confusions between vendors while implementing specifications, sometime components behave differently when ported from one platform to another.

No specific standards but due to abstraction offered by Integration framework, application is easily portable from one framework to another.

7

Coupling

Though JEE defines various components like Servlet, Message Bean, EJB etc to reduce coupling, most of the time code ends up using JEE specific API and gets coupled with JEE. Though off late Java EE 6 offers resource injection features, it seems like Java is actually following what Spring has been providing for many years.

Most of the time there is no need to implement any interface or extend class specific to Spring Integration framework. It follows usual philosophy of Spring framework of writing code in Spring which can be run even without Spring.

 

 

 

 

WHAT’S INSIDE?
SPRING integration is messaging based framework and heavily uses “channel” as a base for transportation. Well though it is messaging based framework it is not tied to JMS. SPRING provides adapters for following
1. FTP
2. File
3. JMS
4. Webservices
5. Email
6. HTTP
7. Stream
8. RMI
Having support for all above connectors ensures that Spring integration framework can be used to connect to legacy as well as cutting edge technologies. Also both ends will never be aware of existence of each other.
Well SPRING integration is not just about transmitting data from one channel to another but it also provides implementation following patterns

S#

 

Description

1

Router

Routes messages to a channel depending upon the type of payload. For e.g. if type of message is String or Long it can be transmitted to a specific channel.
This can be useful if a specific business logic should be executed based on the type of message.

2

Filter

Filters messages sent to input channel and sends it to output channel if filter criteria matches.
Consider an online ecommerce application which can accept orders greater than $150, rest of the orders should be discarded or redirected to another channel.

3

Transformer

To create loose coupling between message consumers and message producers. Provide convenient way to transform messages from one form to another. (There are some transformers available out of the box like object-to-string, payload-serializing-transformer and payload-deserializing-transformer.)

4

Splitter

This is useful when payload has to be broken into multiple parts. For e.g. Order object need to be splitted into multiple line items.

5

Aggregator

Works exactly opposite to Splitter, but somewhat complex as it must identify the logical point where all the splitted messages need to be combined.

6

Resequencer

Works exactly like aggregator but doesn’t process the messages. Resequencer simply resends messages as per the sequence number defined for the message. Messages can be sent as soon as the valid sequence criteria are satisfied or after timeout.

7

Delayer

A simple utility to delay message transmission from input channel to output channel by specific time interval.

 

 

 

Most of the above patterns are meant for Enterprise level integration and also considered as SOA patterns.
Most of you must have implemented solutions that require any of the above functionality. With SPRING integration it’s very easy to develop software that requires any of the above listed functionality.

Well there are few more utilities available in SPRING integration but this article would not be able to describe all of them.

CONCLUSION
Why SPRING Integration?
Anyone looking for integrating two or more systems which use different protocol for communication, SPRING integration looks better choice for development. Following can be attributed as basic reasons for same
1. SPRING integration can be extended and customized adapters can be built as per the requirements. (E.g. SFTP, SSH) Due to its open source nature, the chances of one already available on internet are high.
2. Like SPRING framework, SPRING integration doesn’t require any specific application server rather it can be executed as standalone application. Even if one uses application server, the Spring framework gives benefits of using platform specific features like transaction management.
3. There is also no need of any specific hardware requirement; SPRING integration is lightweight framework that can be deployed on any platform.
4. One can develop a set of components which are loosely coupled and can be executed on demand. (Invocation of component only when a data is available on specific channel. Components need not be aware of channels and can simply deal with POJO objects.)
5. Components can be individually tested and validated. No need of application server would also save great amount of development time spent on frequent stop/start of server. Developers can test the components right from their IDE of choice as standalone application.

REFERENCES
Impressed by SPRING integration project and want to explore more about it, well then here are the resources that helped me to gain knowledge about SPRING integration framework.
http://www.springsource.org/spring-integration (You can download SPRING integration framework from this site.)
http://blog.springsource.com/2009/02/13/982/
http://www.parleys.com/display/PARLEYS/Home#slide=2;talk=29523990;title=Enterprise Integration Patterns with Spring (Well this site has plethora of information available in visual format. Do visit this site, you will always find something useful.)

Posted in: Articles.

Leave a Reply

*