Spring Integration ‘Hello World’ Example

0

In an enterprise world, there are many uses cases where one application talks to another has to interact with another. Spring Integration is a messaging solution which integrates applications using event-driven architecture through lightweight messages. The patterns implemented are based on the popular book Enterprise Application Integration Patterns.

In this article, I will briefly introduce you to the various components involved in spring integration. We will also see a ‘Hello World’ example of Spring Integration.

Dependencies

I will be using Maven as the build tool. You need to add the below artifactIds to your pom.xml.

  1. spring-core
  2. spring-context
  3. spring-integration-core

pom.xml:

<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.javarticles.spring.integration</groupId>
	<artifactId>springIntegrationHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.integration</groupId>
			<artifactId>spring-integration-core</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>
	</dependencies>

	<properties>
		<spring.version>4.1.4.RELEASE</spring.version>
	</properties>

</project>

Spring Integration Components

I have listed here some of the important spring integration components.

    1. Message Wraps the actual payload and contains the message metadata used by Spring Integration
    2. Payload/ The message payload can be any POJO object.
    3. MessageHeaders It contains key/value pairs of properties for message housekeeping.
    4. MessageChannel A message channel is the component through which messages navigate from one end of the application to the other end. Message publishers send messages to the channel, and message consumers receive messages from the channel.
    5. MessageEndPoints A the name suggests they are the components at the endpoints of a channel. They send message to a channel and they are also responsible to consume messages. In other words, when message navigates through a Spring Integration solution, it moves along channels from one endpoint to another

Message EndPoints

Some of the most important end points are listed below:

  1. Channel adapter Is used to connect the message channel to another system or transport.
  2. Transformer Translates message from one format to another so that systems using different formats can communicate together.
  3. Filter Filters some of the data items from the message and allows only those items you are interested in.
  4. Router In a large enterprise with numerous applications, a message may have to go through several channels to reach its final destination. Route component determines ho wto navigate the messag ethrough the channels to reach the final destination
  5. Splitter Split the original message into multiple messages and send them to the appropriate channel.
  6. Aggregator Splitter splits the messages so that a separate service can handle each message. Once all the messages are handled, aggregator can combine multiple messages into one.
  7. Service activator Receives the asynchronous message, determines what service to invoke and what data to pass it, and then invokes the service synchronously.

Spring Integration Hello World Example

We will configure our messaging in the applicationContext.xml file.
We have configured a service-activator which receives messages from channel inputChannel, invokes geeting.sayHello(payload) with the message content passed as an argument.
The value returned by the message is posted to the outbound channel outputChannel. The channel is configured as queue so that it is pollable.

applicationContext.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:int="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
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<int:channel id="inputChannel"/>
	<int:channel id="outputChannel">
	    <int:queue capacity="10"/>
	</int:channel>

	<bean id="springIntHelloWorldExample"
		class="com.javarticles.spring.integration.SpringIntegrationHelloWorldExample">
		<property name="inputChannel" ref="inputChannel" />
	</bean>
	
	<bean id="greeting" class="com.javarticles.spring.integration.Greeting"/>
	
	<int:service-activator input-channel="inputChannel" ref="greeting" output-channel="outputChannel"/>
</beans>

Greeting:

package com.javarticles.spring.integration;

public class Greeting {
    public String sayHello(String payload) {
        return "Hello World!" + payload;
    }
}

To initiate the routing, we post a message to the inbound channel inputChannel.

Message message = MessageBuilder.withPayload(payload).build();
inChannel.send(message);

The message will navigate through the routing configured in applicationContext.xml. At the end of the route, a message will be posted to the outbound channel outputChannel. Next, we will poll the message from the outbound channel just to make sure the service activator has sent the message.

QueueChannel outChannel = (QueueChannel) context.getBean("outputChannel");
System.out.println(outChannel.receive());

SpringIntegrationHelloWorldExample:

package com.javarticles.spring.integration;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

public class SpringIntegrationHelloWorldExample {
    private MessageChannel inChannel;

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            SpringIntegrationHelloWorldExample springIntExample = (SpringIntegrationHelloWorldExample) context
                    .getBean("springIntHelloWorldExample");
            springIntExample.post("This is spring integration example.");
            QueueChannel outChannel = (QueueChannel) context.getBean("outputChannel");
            System.out.println(outChannel.receive());
        } finally {
            context.close();
        }
    }

    public void post(String payload) {
        Message message = MessageBuilder.withPayload(payload).build();
        inChannel.send(message);
    }

    public void setInputChannel(MessageChannel in) {
        this.inChannel = in;
    }
}

Output:

GenericMessage [payload=Hello World!This is spring integration example., headers={id=73b77c1c-5637-67e9-3e2d-2b0f87867e27, timestamp=1430460495598}]

Download the source code

This was a ‘Hello World’ example of Spring Integration. You can download the source code here: springIntegrationHelloWorld.zip

About Author

Ram’s expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring.
You can reach him at [email protected]

Comments are closed.