Click here to Skip to main content
11,790,613 members (58,011 online)
Click here to Skip to main content

Spring Integration with ActiveMQ for POJO Based Service

, 25 Aug 2010 GPL3 45.6K 1K 3
Rate this:
Please Sign up or sign in to vote.
How to use Spring Integration with ActiveMQ to handle message in pure POJO


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.


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.


  • 25th August, 2010: Initial post


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


About the Author

anand kr
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.

You may also be interested in...

Comments and Discussions

Generalgood intro for spring integration Pin
AnvithKarkera8-Feb-11 7:38
memberAnvithKarkera8-Feb-11 7:38 
GeneralSpring Integration API Pin
Kshitiz Garg30-Aug-10 20:27
memberKshitiz Garg30-Aug-10 20:27 
GeneralRe: Spring Integration API Pin
anand kr31-Aug-10 1:12
memberanand kr31-Aug-10 1:12 

The advantages are clear. Though you can easily send and receive message using Spring JMS support, there are lot of other things you may not be able to achieve easily, namely: transformation, routing, filtering. Another big advantage is POJO support, so your code almost is unaware of message broker, webservice or any other external entity.

Please have a look at Enterprise Integration Patterns which is the basic idea/force behind Spring Integration technology.

GeneralRe: Spring Integration API Pin
Kshitiz Garg31-Aug-10 1:19
memberKshitiz Garg31-Aug-10 1:19 
GeneralRe: Spring Integration API Pin
Kshitiz Garg3-Sep-10 3:44
memberKshitiz Garg3-Sep-10 3:44 
GeneralRe: Spring Integration API Pin
anand kr3-Sep-10 6:40
memberanand kr3-Sep-10 6:40 
GeneralGreat help ! Pin
Kshitiz Garg30-Aug-10 20:20
memberKshitiz Garg30-Aug-10 20:20 
GeneralSemms to me good Pin
Md. Marufuzzaman24-Aug-10 18:48
mvpMd. Marufuzzaman24-Aug-10 18:48 
GeneralRe: Semms to me good Pin
swathichandrika14-Sep-10 0:14
memberswathichandrika14-Sep-10 0:14 
GeneralRe: Semms to me good Pin
anand kr14-Sep-10 4:54
memberanand kr14-Sep-10 4:54 
GeneralRe: Semms to me good Pin
swathichandrika14-Sep-10 5:23
memberswathichandrika14-Sep-10 5:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1509028.1 | Last Updated 25 Aug 2010
Article Copyright 2010 by anand kr
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid