Spring Integration – Session 1 – Hello World

The ” Hello World” of Spring Integration – consider a simple program to print “Hello World” to the console using Spring Integration and in the process visit a few Enterprise Integration Patterns Concepts

Before jumping into the program itself, a quick review of messaging concepts will be helpful – Messaging is an Integration style where two independent applications communicate with each other through an intermediary – the intermediary is referred to as the “Messaging System”.

Enterprise Integration Patterns describes the common Integration related issues with Messaging based application Integration and their recommended solutions.

For eg. Consider one of the Enterprise Integration Patterns – The Messaging Channel, to quote from the Entprise Integration Patterns Book:

The problem “Messaging Channel” is trying to solve is:

An enterprise has two separate applications that need to communicate, preferably by using Messaging.
How does one application communicate with another using messaging?

The solution is:

Connect the applications using a Message Channel, where one application writes information to the channel and the other one reads that information from the channel.
All the other Enterprise Integration Patterns are described along the same lines.

The reason to quickly visit Enterprise Integration Patterns is to set the context – Spring Integration aligns very closely with the Enterprise Integration Patterns and is the “Messaging system” that was mentioned earlier.

So now to see the Hello World using Spring Integration:

First a small junit:

public class HelloWorldTest {
 MessageChannel messageChannel;

 public void testHelloWorld() {
  Message<String> helloWorld = new GenericMessage<String>("Hello World");

Here a MessageChannel is being wired into the test, the first application (here the Junit), sends a Message(in this case a string “Hello World”) to the Message Channel, something reads the message from the “Message Channel” and writes the message to the system out.

Now, let us see the remaining part of how “something” picks up a message from the Message Channel and writes to the system out:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
  http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.1.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <int:channel id="messageChannel"></int:channel>
 <int-stream:stdout-channel-adapter channel="messageChannel" append-newline="true"/>


The above is a Spring Integration flow being described using Spring Custom namespaces, here the Integration Namespace. A “Message Channel”, imaginatively called the “messageChannel” is created, a “Hello World” “Message” is placed into the “Messsage Channel”, from which a “Channel Adapter” gets the message and prints it to the standard out.

This is a small program, but it uses three Enterprise Integration Patterns – the Message (“Hello World”, which is the packet of information being sent to the messaging system, the ” Message Channel” which was introduced earlier and the new one, the Channel Adapter, here an Outbound Channel Adapter which connects the messaging system to the application(in this case the system out). It further shows how Spring Integration aligns very closely with the Enterprise Integration Patterns terminology with its Spring custom namespace.

This simple program introduces Spring Integration, I will be introducing Spring Integration in more detail using a few more samples in the next few sessions.

1. Spring Integration Reference: http://static.springsource.org/spring-integration/reference/htmlsingle/
2. Enterprise Integration Patterns: http://www.eaipatterns.com/index.html
3. Visio templates for EIP : http://www.eaipatterns.com/downloads.html

Reference: Spring, Spring Integration, Enterprise Development from our JCG partner Biju Kunjummen at the all and sundry blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

eight − = 2

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books