Each Spring Integration application is completely embedded, and needs no server infrastructure. In fact, a Spring Integration application can be deployed inside another application—for example, in your web application endpoint.


Spring Integration is deployed into your application; your application is not deployed into Spring Integration, as you might expect. There are no start and stop scripts and no ports to guard.


The simplest possible working Spring Integration application is a simple Java public static void main method to bootstrap a Spring context,

App.java


package com.apress.prospringintegration;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:spring-context.xml");
context.start();
}
}




A standard Spring application context has been created and started.


The configuration file will be created for creating a simple input channel, a service activator to respond to a message published to the channel, and an output channel where the service activator sends it return value.

spring-context.xml integration graph




spring-context.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:si="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.apress.prospringintegration"/>
<int:channel id="input"/>
<int:channel id="output">
<int:queue capacity="10"/>
</int:channel>

<int:service-activator input-channel="input" output-channel="output" ref="messageHandler"/>

</beans>






A point-to-point direct channel input is created using the channel element, and a queue channel output is created by adding the queue element. In addition, a service activator messageHandler is created using the service-activator element.


Spring Integration supports annotations for configuring Spring bean components. By adding the component-scan element of the context library, any Java class in the package com.apress.prospringintegration with the annotation @Cmponent will be configured as a Spring bean.


By convention, the bean named messageHandler will refer to the class MessageHandler, resolved by capitalizing the first letter of the bean name. Thus, if a message is sent the
channel input, it will be forwarded to the service activator class MessageHandler. Since no method has been configured in the Spring configuration class, the message will be directed to the method annotated with @ServiceActivator.

MessageHandler.java


package com.apress.prospringintegration;

import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.stereotype.Component;
@Component
public class MessageHandler {
@ServiceActivator
public String handleMessage(String message) {
System.out.println("Received message: " + message);
return "MESSAGE:" + message;
}
}



Note that the MessageHandler class has the required annotations for component scanning and the service activator annotation that directs the input message to the handleMessage method.


This is a good time to describe the convention for mapping the Message object to the method signature. Typically, the method signature will either map to the Message payload or the Message object itself.


the method signature maps to the String payload. The message payload will be logged and forwarded to the output channel, and prefixed with MESSAGE:.

App.java


package com.apress.prospringintegration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;

import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.support.MessageBuilder;
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:spring-context.xml");
context.start();
MessageChannel input =
(MessageChannel) context.getBean("input", MessageChannel.class );
PollableChannel output =
(PollableChannel) context.getBean("output", PollableChannel.class );
input.send(MessageBuilder.withPayload("Pro Spring Integration Example").build());
Message<?> reply = output.receive();
System.out.println("received: " + reply);
}
}

Maven Project Structure














Run application in Eclipse











Results of Running the Example Code


Received message: Pro Spring Integration Example
received: [Payload=MESSAGE:Pro Spring Integration Example][Headers={timestamp=1296618370609,id=d4c9fac6-655b-45f3-aa4e-e02c1167eb74}]

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -