Click here to Skip to main content
Click here to Skip to main content

Spring Integration with ActiveMQ for POJO Based Service

, 25 Aug 2010 GPL3
Rate this:
Please Sign up or sign in to vote.
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)

Share

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.
Follow on   Twitter

Comments and Discussions

 
Generalgood intro for spring integration PinmemberAnvithKarkera8-Feb-11 8:38 
GeneralSpring Integration API PinmemberKshitiz Garg30-Aug-10 21:27 
GeneralRe: Spring Integration API Pinmemberanand kr31-Aug-10 2:12 
GeneralRe: Spring Integration API PinmemberKshitiz Garg31-Aug-10 2:19 
GeneralRe: Spring Integration API PinmemberKshitiz Garg3-Sep-10 4:44 
GeneralRe: Spring Integration API Pinmemberanand kr3-Sep-10 7:40 
GeneralGreat help ! PinmemberKshitiz Garg30-Aug-10 21:20 
GeneralSemms to me good PinmvpMd. Marufuzzaman24-Aug-10 19:48 
GeneralRe: Semms to me good Pinmemberswathichandrika14-Sep-10 1:14 
GeneralRe: Semms to me good Pinmemberanand kr14-Sep-10 5:54 
GeneralRe: Semms to me good Pinmemberswathichandrika14-Sep-10 6:23 
Hi
1.I installed Activemq
2.created a queue in admin console for storing the messages that i need to send to my application
3.I used the above code for creating the Listener
4.I made changes in activeMq.xml like this
<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616"/>
</transportConnectors>
5.and spring changes also i made in the file below is the code jmsservicecontext.xml(file in my project)
ENROLLMENT.RESPONSE.Q is the Queue i created in admin of ActiveMQ
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"
default-autowire="byName">

<!-- Begin RSA JMS Settings -->
<bean id="rsaClient" class="com.hsc.enrollment.rsa.RsaClientImpl">

<property name="enrollment" ref="enrollment" />
<property name="enrollmentEnv">
<value>${enrollment.service.environment}</value>


</property>
</bean>

<!-- Processes the messages from RSA Topic Queue -->
<bean id="rsaMessageListener" class="com.hsc.enrollment.jms.JmsRsaClientImpl">
<property name="waitTime">
<value>${enrollment.service.rsa.messagelistener.wait}</value>
</property>
</bean>


<!-- The Transaction Manager -->
<bean id="rsaJmsTransactionManager"
class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="rsaTopicConnectionFactory" />
</bean>

<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
 
<!-- ActiveMQ Setup -->
<!-- applies -->
<bean id="rsaResponseConnectionFactoryTarget" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>${enrollment.service.rsa.broker.url}</value>
</property>
</bean>

<!-- applies -->
<!-- User Authentication for Queues -->
<bean id="rsaResponseConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="rsaResponseConnectionFactoryTarget" />
<property name="username" value="${enrollment.service.rsa.usesrname}" />
<property name="password" value="${enrollment.service.rsa.password}" />
</bean>


<!-- applies -->
<bean id="rsaResponseDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="ENROLLMENT.RESPONSE.Q" />
</bean>



<!-- applies -->
<!-- The RSA Response Queue -->
<bean id="jmsRsaTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="rsaResponseConnectionFactory" />
<property name="defaultDestination" ref="rsaResponseDestination" />
</bean>



<bean id ="jmsMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="rsaResponseConnectionFactory" />
<property name="defaultDestination" ref="rsaResponseDestination" />
<property name="rsaMessageListener" ref="rsaMessageListener" />
<property name="sessionTransacted" value="false" />
<property name="concurrentConsumers" value="100" />
<property name="recoveryInterval" value="3000" />
</bean>

<!-- End RSA JMS Settings -->
</beans>
 

6.when iam sending the message to this queue its not working Cry | :((
is there any other change that i should be doing to execute this and able to send messages to this Listener
 
any help would be greatly appreciated
I am breaking my head to get it work please help me Confused | :confused:
 
Thanks in advance Smile | :)

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
Web02 | 2.8.1411028.1 | Last Updated 25 Aug 2010
Article Copyright 2010 by anand kr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid