Click here to Skip to main content
15,397,498 members
Articles / Programming Languages / Java
Posted 25 Aug 2010


3 bookmarked

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

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 
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
professionalMd. 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    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.