Getting started with Spring Integration

In this post, we will see what Spring Integration is about and how to get started with it.

What is Spring Integration?

Spring Integration is an open source framework for enterprise application integration. It is built on top of Spring framework. Hence it is very easy to adopt Spring Integration in the projects which are already using the Spring framework.

Spring Integration facilitates asynchronous, message driven behavior within a Spring based application. Spring Integration components are loosely coupled for modularity and testability.

Main components of Spring Integration

We will see some of the main components of Spring Integration which is the core of the framework.

Message

In Spring Integration, Message is a generic wrapper for any java object combined with metadata used by the framework while handling that object. It consists of payload and headers. Payload can be of any type and the headers hold commonly required information such as id, timestamp, correlation id and return address.

Message Channel

A Message Channel represents the pipe of the pipe-and-filters architecture. Producers send message to a channel and consumers receive message from a channel. The Message Channel therefore decouples the messaging components and also provides a convenient point for interception and monitoring of Messages.

Message Endpoint

A Message Endpoint represents the filter of a pipes-and-filters architecture. The endpoint’s primary role is to connect application code to messaging framework and to do so in a non-invasive manner. Some of the endpoints are Transformer, Router, Splitter, Aggregator, Service Activator, Gateways.

Getting started

We will now create a simple Hello World application using Spring Integration.

Prerequisite tools and libraries

1. Maven 3
2. Eclipse IDE 4.4.2
3. JDK 1.6 or higher
4. Spring Integration Core 4.3.9.RELEASE
5. Spring Integration Stream 4.3.9.RELEASE

Step 1 : Create a simple maven project in Eclipse IDE. Add the following dependencies to pom.xml

Step 2 : Now add the Spring configuration file spring-integration-config.xml in src/main/resources. This should have enabled  the Spring project nature as well in the Eclipse IDE.

Spring Integration graph :

Note : In order to see the Spring Integration graph in Eclipse IDE, make sure that the spring plugin for eclipse is installed.

Step 3 :  Declare the Interface to have methods for processing the message.

Step 4 :  Now, lets code the Simple message processor implementation.

If you have noticed the service-activator declaration, it will pass on the message to printChannel.

From printChannel, the message will be passed on to stdout-channel-adapter. So the message returned from addMessageGreetings method will be directed to the standard output console.

Lets see this in action

Step 5 :  Create a GreetingsApp.java class as below

Execute the GreetingsApp.java and you should see the below output printed to console.

So, how this works?

When we send the String message to channel component inputChannel, the message is then passed on to service activator.

In the service activator, we can write the business related code. In our case, we just referred to the SimpleMessageProcessor instance where we created the greetings message.
The returned message from service activator is then passed on the printChannel.

The printChannel forms the input for stdout-channel-adapter as per our configuration. Hence the greeting message will get printed to the console.

If we take a look in to the GreetingsApp.java, which is the client class from where we send the spring integration message, we are dealing with the spring integration components directly. We are depending on the use of the channel component inputChannel to send the message.

We can remove those direct dependencies using the component gateway proxy to our configuration.

Step 6 : Integrating using Gateway Proxy

Add the below component entry to configuration xml.

Spring Integration graph :

You should now see the additional gateway components in the Spring Integration graph.

For the service-activator component, GatewayMessageProcessor.java will implement the MessageProcessor interface and do the same work of creating the greeting message. In this case, its a different greeting message.

Step 7 : Now create a client class GatewayGreetingsApp.java

When the GatewayGreetingsApp.java is executed you should see the below output printed to console.

If you note the configuration for gateway proxy, we are not directing the output of service activator to stdout-channel-adapter. Instead the message is passed on to the gatewayOutputChannel from which the gateway will listen to and receive the response.

Then in GatewayGreetingsApp.java, response message is received of type Message<String>. The output is then printed using System.out.

In real time cases, the response can be used for performing any other activities like passing to another spring integration component, logging to database.
In our example, we just printed it to console using System.out.

Conclusion

This ends our tutorial on getting started with spring integration. You can refer the Spring Integration API documentation to dig deep and find out what the Spring Integration offers to solve the integration problems.

Hope this post helped you in getting started with spring integration. If you have any questions or concerns please post it in the comments section.

Thank you!


Download source code : spring-integration-sample.zip


About Vignesh M

Java developer , AWS Certified Solutions Architect Associate and Cloud technology enthusiast. Currently working for Dxc technology. He believes that knowledge increases by sharing not by saving.

8 thoughts on “Getting started with Spring Integration

Leave a Reply

Your email address will not be published. Required fields are marked *