ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

How to use spring framework setup for JMS topic and listener

Updated on June 26, 2012

Below is the code sample to set up a JMS topic using spring framework.

1. Need to define a jms listener container which would contain the connection factory with the destination type as "topic". Also define the jms listener for it.

2. define the jms connection factory for topic as shown below.

3.The jms listener defined in the step 1 is messageListenerAdapter which should be defined with the properties delegate , defaultListenerMethod and messageConverters.

4. define the messageListener and message converters also as below. The delegate in the step 3 refers to the messageListener.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""	

    <!-- JMS Queue Connection Factory -->
    <bean id="jndiJmsTopicConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">     
        <property name="jndiName" value="jms/myConFactory" />
		<property name="lookupOnStartup" value="false"/>
		<property name="cache" value="true" />
		<property name="proxyInterface"  value="javax.jms.TopicConnectionFactory"/>
    <!-- Spring JMS Topic Connection Factory -->
   <bean id="jmsTopicConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory" ref="jndiJmsTopicConnectionFactory" />
	<!-- Message Driven POJO -->
	<bean id="messageListener" class="com.home.MyEventListener" autowire="byName"/>		
	 <alias alias="unMarshaller" name="jaxb2Marshaller"/>
	  <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">        
        <property name="contextPath" value=""/>
	<bean id="messageConverter" class="com.home.MyEventMessageConverter" autowire="byName"/>
	<bean id="messageListenerAdapter" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
		<property name="delegate" ref="messageListener" />
		<property name="defaultListenerMethod"	value="processMyMessage" />
		<property name="messageConverter" ref="messageConverter" />
	<jms:listener-container connection-factory="jmsTopicConnectionFactory" destination-type="topic" container-type="default">
	    <jms:listener destination="STATUS" ref="messageListenerAdapter" method="processMyMessage"/>

When the message is received by the messageListenerAdapter, it will call the default listener method defined for the deletage if it implements the messageListener interface.

If the delegate does not implement the messageListener interface, then the fromMessage() method will be called from the messageConverter defined for it. The messageConverter should implement the MessageConverter interface which defines the methods fromMessage() and toMessage() methods. The code shown above is for the case where the message would reach the converter first. The messageConverter would return the message which would then be passed to the default listener method defined for the messageListener.


    0 of 8192 characters used
    Post Comment

    No comments yet.