Tuesday, December 18, 2012

Spring Integration

OK lets do this. In this post I will be guiding you through a simple example on Spring Integration. Spring integration, no matter how wild it appears to be, at its heart, its a messaging framework that supports both application wide messaging and with a bit of work (not too much), across the enterprise too. Its light weight and moreover you get all this with separation of concerns enabling you to write extensible, easy to maintain applications.

Before we go any further let me first run you through existing integration patterns or styles.

  1. Shared Database Integration
  2. Remote Procedure Calls
  3. File Based Integration
  4. Message based integration
I am sure most of you are familiar with these styles. Spring integration however is based on message based integration, although you would find support for others in the framework.

Lets now see what Spring Integration is about. Here is a few things you need to understand the example that's about to follow.

Messages

Messages are units of information, information that you will be exchanging within/between applications or to be general endpoints. There are no restrictions on what your message content can be. It could be XML, plain strings, you can have header content too.

Message Channels

Message channels are a medium of transportation, if  you will. Channels manage message delivery, thereby decoupling senders (producers) from receivers(consumers) of messages and moreover from any concerns about the state of the consumer (passive or active) and delivery mechanism.

Message Endpoints

Message endpoints can be applications or application components. They are the ones doing something with the messages. The could be publishers, consumers or components sitting in the middle relaying/ aggregating/ splitting messages. There are different types on endpoints in Spring Integration, in this example we will be using service-activator and messaging gateway.
Service Activator
A service Activator simple is a component that takes an input from a channel and invokes a service and returns a message (more correctly an outbound message) based on an incoming message. The beauty of this is that the service would be completely unaware that its a part of a messaging system.
Messaging Gateway
Messaging gateway on the other hand is in simple terms an entry point or an exit point of a messaging flow. You can define both inbound and outbound messaging gateways, outbound messaging gateways allow you to connect to JMS, Web Services etc. Gateways when declared with XML allows your code to be completely void of any spring integration related details.

And now for the fun part.

Lets obey the unwritten traditions of programming and create a much cliche'd Hello World Application, only this time using Spring Integration.

Following is the structure of the project,

As for the pom file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.spring.test</groupId>
 <artifactId>SpringIntegration</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>SpringIntegration</name>
 <dependencies>
  <dependency>
   <groupId>org.springframework.integration</groupId>
   <artifactId>spring-integration-core</artifactId>
   <version>2.1.3.RELEASE</version>
  </dependency>
 </dependencies>
</project>

Here is the service interface

package com.spring.integration;

public interface HelloService {
 String sayHello(String name);
}

 
note that it returns a value, you can have service definitions with void types too.

as for the service implementation we have a simple POJO completely oblivious of its participation in a messaging system.

package com.spring.integration.services;

import com.spring.integration.HelloService;

public class MyHelloService implements HelloService {

 public String sayHello(String name) {
  return ("Hello " + name);
 }

}

lets have a look at the spring configuration file shall we

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/integration"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">


 <gateway service-interface="com.spring.integration.HelloService"
  id="helloGateway" default-request-channel="names" />
  
 <channel id="names" />
 
 <service-activator input-channel="names" ref="helloService"
  method="sayHello" />
  
 <beans:bean id="helloService"
  class="com.spring.integration.services.MyHelloService" />
  
</beans:beans>

That's is it now lets run this thing shall we.

package com.spring.integration.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.integration.HelloService;

public class Tester {

 public static void main(String[] args) {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    "classpath:spring-beans.xml");
  HelloService helloService = applicationContext.getBean("helloGateway",
    HelloService.class);
  System.out.println(helloService.sayHello("World"));

 }

}

See how your code is completely void of any spring integration details, the gateway definition allows us to interact with the service just like you interact with any other spring bean.

for the complete source
Click here

1 comment:

  1. Thanks for writing a post on Spring Integration.
    I really loved the flexibility and the configurabilty provided by Spring Integration. Its really a powerful tool for the realtime processing.
    Nice Post!!!

    ReplyDelete