Tuesday, May 10, 2011

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. :)



 

10 comments:

  1. Hey, it works! It's the first simple sample I found that it really works! ¿Do you have more examples?

    ReplyDelete
  2. https://github.com/SpringSource/spring-integration-samples

    Regards.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thank you, I agree a nice little example that just works

    ReplyDelete
  5. Thanku...its working and helped me alot

    ReplyDelete
  6. thank u very much, it is wonderfull, it is worthy to read it and to do it, you are smart, thnx

    ReplyDelete
  7. Ibrahim,

    Thank you very much, it is really the best Example: I have visited many websites, but your Example is the best. Thank you very much , go more
    do more, and dont give up, we need more examples,

    good job :)

    ReplyDelete
  8. Ibrahim,

    Thank you very much, it is really the best Example: I have visited many websites, but your Example is the best. Thank you very much , go more
    do more, and dont give up, we need more examples,

    good job :)

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)