Introduction to Spring Integration
Introduction
The objective of this post to provide introduction to Spring Integration. Spring Integration is most popular messaging framework is being used day-to-day and extends the spring programming model to support the Enterprise Integration Patterns (EIP). It is based on pipe and filter design architecture.
Spring Integration framework build upon core spring framework and supports same kind of configuration style, XML, annotation and programming etc.
This framework consists of 3 components, that is
- Messages
- Channels
- Endpoints
How Spring Integration framework work?
This framework works on mechanism of sending and receiving lightweight messages. Here are the steps :-
- Message builder create a message contain information called payload.
- Then producer send message to a channel (represents pipe in "pipe and filter" architecture ) and consumer receive message from the channel.
- Message channel may follow either point-to-point or publish-subscribe semantics like JMS.
- Endpoint represents filter in "pipe and filter" architecture.
- Endpoint perform some action based on payload, or could route the message to another endpoint to do the action.
First Spring Integration Program
Step.1: Install maven
Step.2: Install eclipse
Step.3: Download Spring integration jars
Step.4: Do the eclipse setup for new maven project
Step.5: Run SpringIntegrationMain program
Project Structure in Eclipse
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:integration="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
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">
<context:annotation-config />
<context:component-scan base-package="com.manoja.brahma.learnings"/>
<integration:channel id="inputChannel"/>
<integration:service-activator input-channel="inputChannel" ref="springIntegrationService" method="greet" output-channel="outputChannel"/>
<integration:channel id="outputChannel"/>
<integration:service-activator input-channel="outputChannel" ref="springIntegrationDisplay"/>
</beans>
SpringIntegrationMain.java
package com.manoja.brahma.learnings;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
/**
* This main class creates input channel and sends Spring Integration greetings message to the channel
*
* @author Manoja Brahma
*/
public class SpringIntegrationMain {
public static void main( String[] args ) {
//Get the context instance
final ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
// get the message channel reference from the application context
final MessageChannel channel = context.getBean("inputChannel", MessageChannel.class);
// create a message with the content "World" through MessageBuilder withPayLoad()
final Message<String> message = MessageBuilder.withPayload("Spring Integration").build();
// send the message to the inputChannel
channel.send(message);
}
}
SpringIntegrationService .java
package com.manoja.brahma.learnings;
import org.springframework.stereotype.Service;
/**
* This service modify the original message and return the new message to the output channel
*
* @author Manoja Brahma
*/
@Service
public class SpringIntegrationService {
public String greet(String name) {
return "First " + name + " Program";
}
}
SpringIntegrationDisplay .java
package com.manoja.brahma.learnings;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Service;
/**
* This service consume the message from the channel and print it on the console
*
* @author Manoja Brahma
*/
@Service
public class SpringIntegrationDisplay {
@ServiceActivator
public void display(String value){
System.out.println(value);
}
}
Thank you
Manoja Brahma