How to use spring framework setup for JMS topic and listener

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="http://www.springframework.org/schema/beans"	
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/aop
		   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		   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.xsd
           http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">



	
    <!-- 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"/>
    </bean>
    
    
    <!-- Spring JMS Topic Connection Factory -->
   <bean id="jmsTopicConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory" ref="jndiJmsTopicConnectionFactory" />
    </bean>   
    
	
	<!-- 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="com.home.events.contentObjects"/>
    </bean>
	
	<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" />
		</bean>	
      
	<jms:listener-container connection-factory="jmsTopicConnectionFactory" destination-type="topic" container-type="default">
	    <jms:listener destination="STATUS" ref="messageListenerAdapter" method="processMyMessage"/>
	</jms:listener-container>
  </beans>

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.

More by this Author


Comments 1 comment

gepeTooRs 9 months ago

is only a short drive.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working