Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Spring Integration with ActiveMQ for POJO Based Service

5.00/5 (1 vote)
25 Aug 2010GPL33 min read 85.3K   1.1K  
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)