Tuesday, October 9, 2012

Spring Integration Messaging tutorial, Spring Integration in 10 Minutes

There are many things in Spring Integration:

1. Messaging
2. Routing
3. Mediation
4. Invocation
5. CEP (Complex Event Processing)
6. File Transfer
7. Shared database
8. Remote Procedure call

Here I am posting Spring Integration Messaging (Kind of JMS) example here:

Create one project in Eclipse say SpringIntegrationDemo and add these below jar file to that project:

1. spring-core-3.0.5.RELEASE.jar
2. spring-integration-core-2.0.0.BUILD-SNAPSHOT.jar
3. jar/commons-logging-1.1.jar
4. spring-context-3.0.5.RELEASE.jar
5. spring-beans-3.0.5.RELEASE.jar
6. spring-asm-3.0.5.RELEASE.jar
7. spring-expression-3.0.5.RELEASE.jar
8. spring-aop-3.0.5.RELEASE.jar
9. aopalliance-1.0.jar

In src folder, create these three files:
1. MyService.java
2. myServiceDemo.xml
3. MyServiceDemo.java

MyService.java

public class MyService {

 public String sayHello2(String name) {
  return "Suman Hello :  " + name;
  }

}

myServiceDemo.xml

<?xml version="1.0" encoding="UTF-8"?>
&it;beans:beans xmlns="http://www.springframework.org/schema/integration"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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/integration
   http://www.springframework.org/schema/integration/spring-integration.xsd" >

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

 <service-activator input-channel="inputChannel"
                    output-channel="outputChannel"
                    ref="myService"
                    method="sayHello2"/>
 <beans:bean id="myService" class="MyService"/>
</beans:beans>

MyServiceDemo.java

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
public class MyServiceDemo {

 public static void main(String[] args) {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext("myServiceDemo.xml", MyServiceDemo.class);

  MessageChannel inputChannel =  context.getBean("inputChannel", MessageChannel.class);
  PollableChannel outputChannel =  context.getBean("outputChannel", PollableChannel.class);

// Just senging messages into message channel.
  for(int i=0;i<10;i++){
     inputChannel.send(new GenericMessage<String>("World : "+(i+1)));
  }

  // Getting message from message channel
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());

 }
}

Now you can run MyServiceDemo file, will get result. :)

0 comments:

Post a Comment