Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Java

Spring Integration with ActiveMQ for POJO Based Service

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Aug 2010GPL33 min read 78.7K   1.1K   3   11
How to use Spring Integration with ActiveMQ to handle message in pure POJO

Introduction

There are a variety of ESBs available to handle medium to large scale messaging needs. Spring Integration is the latest of all of them. Backed by strong support from Spring Community and well established Dependency Injection feature, Spring integration(SI) can be used as a very strong middleware tool. It supports routing and transformation of messages so that different transports and different data formats can be integrated without impacting testability and scalability.

Background

The idea of Spring Integration comes from Enterprise Integration Patterns, by Gregor Hohpe, that categorizes and explains many of the integration patterns common to integration solutions.

Two best places to learn about Spring Integration are:

  1. Getting Started with Spring Integration by Joshua Long
  2. Spring Integration Reference Manual

Also have a look at Mark Fisher's blogs.

The sample application is a small demonstration of what can be achieved using Spring Integration. For example, it shows how easily A POJO Gateway can be setup to communicate with an ActiveMQ Message Queue. From code side, the developer is relieved from JMS intricacies and left with simple XML configurations. The code remains pure POJO. Also note that Service itself is a POJO. All we have is a set of transformers/routers/endpoints/channels.

The example shows how a SOAP message can be sent over MQ and can be handled without any difficulty using XSLT/XPath support from Spring Integration. Also employed is Marshalling/Unmarshalling using Spring OXM and Castor.

The SOAP Message is sent through: POJO Gateway > JMS OutBoundGateway (SI) > ActiveMQ > JMSInboundGateway(SI) > XSLT Router(SI) > XSLT Payload Transformer(SI) > POJO Service and reply traces back the same path.

One thing you will notice is that Server(JMSInboundGateway)/Client(JMSOutboundGateway) are in same application, though this is for simplicity and can be easily separated out.

Using the Code

The code has been developed using Maven 2.0 and all you need to do is Install Maven. It will take care of all dependencies.

Download the project and import in Eclipse IDE as a new Java Project. If you have maven-plugin installed in Eclipse IDE, then the project can be build very easily in Eclipse itself.

Otherwise on command line, in the Project folder where pom.xml is present, run the following command:

mvn clean install 

Little details of code:

The main flow of the application is contained in context.xml, which is loaded by ClassPathApplicationContext.

GatewayProxyFactoryBean does all the work of creating the proxy of SampleGateway and invoking the method serve() on the proxy and sending argument to JMSOutboundGateway.

The OutBoundGateway then delivers it to ActiveMQ.

On the other end, JMSInboundGateway waits for the requestQueue, and traps this request to transfer the SOAP XML message to SI transformers and routers to convert it to POJO request.

The request is handled by Service.add(Data) and the response is put back on the inboundGateway which delivers it back to ActiveMQ.

The ActiveMQ transfers the response back to client JMSOutboundGateway which transfers it back to SampleGateway as a return value.

Points of Interest

Exception handling has not been done, but can be done very easily using Exception Mapper for Gateways and has been left as an exercise.

History

  • 25th August, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
India India
Specialization in Java/J2EE/JSF/Richfaces/Spring/Spring Integration
Area of interest: Refactoring with design patterns.
Currently working on Spring Integration and ActiveMQ.

Comments and Discussions

 
Generalgood intro for spring integration Pin
AnvithKarkera8-Feb-11 7:38
AnvithKarkera8-Feb-11 7:38 
GeneralSpring Integration API Pin
Kshitiz Garg30-Aug-10 20:27
Kshitiz Garg30-Aug-10 20:27 
GeneralRe: Spring Integration API Pin
anand kr31-Aug-10 1:12
anand kr31-Aug-10 1:12 
GeneralRe: Spring Integration API Pin
Kshitiz Garg31-Aug-10 1:19
Kshitiz Garg31-Aug-10 1:19 
GeneralRe: Spring Integration API Pin
Kshitiz Garg3-Sep-10 3:44
Kshitiz Garg3-Sep-10 3:44 
GeneralRe: Spring Integration API Pin
anand kr3-Sep-10 6:40
anand kr3-Sep-10 6:40 
GeneralGreat help ! Pin
Kshitiz Garg30-Aug-10 20:20
Kshitiz Garg30-Aug-10 20:20 
GeneralSemms to me good Pin
Md. Marufuzzaman24-Aug-10 18:48
professionalMd. Marufuzzaman24-Aug-10 18:48 
GeneralRe: Semms to me good Pin
swathichandrika14-Sep-10 0:14
swathichandrika14-Sep-10 0:14 
Hi iam also working with Spring Integration with activemq can you tell me what are all the steps to follow while integrationg active mq with maven project
I have done lot of googling but is'nt of much help is there
any help is greatly appreciated

here is my 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hsc.enrollment</groupId>
<artifactId>enrollment-service</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>enrollment-service</name>
<description>Enrollment Service</description>

<parent>
<groupId>com.hsc.xtend</groupId>
<artifactId>common-config-pom</artifactId>
<version>2.1-8</version>
</parent>


<properties>
<cxf.version>2.1.4</cxf.version>
</properties>

<scm>
<connection>scm:svn:http://hsc-javadev1.hscil.com/svn/enrollment/enrollment-service/trunk</connection>
<url>scm:svn:http://hsc-javadev1.hscil.com/svn/enrollment/enrollment-service/trunk</url>
</scm>

<dependencies>
<!-- Add dependencies here as the project grows -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>activemq-all</groupId>
<artifactId>activemq-all</artifactId>
<version>5.3.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>activemq-pool</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.3.1</version>
<scope>runtime</scope>
</dependency>

<!-- <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.3.1</version>
</dependency> -->

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>

<build>
<finalName>enrollment-service</finalName>
</build>
</project>

waiting for early reply Cry | :((
GeneralRe: Semms to me good Pin
anand kr14-Sep-10 4:54
anand kr14-Sep-10 4:54 
GeneralRe: Semms to me good Pin
swathichandrika14-Sep-10 5:23
swathichandrika14-Sep-10 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.