Tuesday, February 19, 2013

Spring MDP Java example

1. MDP code
package com.xyz.web.test;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

/**
 */
public class QueueListenerBean implements MessageListener {

private static final Logger LOG = Logger.getLogger(QueueListenerBean.class);

  /**
   * Retrieve the int value of the TextMessage and
   * increment the RMI counter by that much.
   */
  public void onMessage(Message msg) {
      LOG.info("In OnMessage()");
    try {
        TextMessage textMessage = (TextMessage) msg;
        System.out.println("Message received:" + textMessage.toString());
        LOG.info("Message received:" + textMessage.toString());
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
}

2. Spring Application Context file
<?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:jee="http://www.springframework.org/schema/jee"
  xsi:schemaLocation="
  http://www.springframework.org/schema/jee
  http://www.springframework.org/schema/jee/spring-jee.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">

<jee:jndi-lookup id="myConnectionFactory" jndi-name="jms/entryQueueCXF"
proxy-interface="javax.jms.QueueConnectionFactory" cache="false"
resource-ref="false" lookup-on-startup="true" />

<bean id="myMessageListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="myConnectionFactory" />
<property name="destinationResolver" ref="jndiDestinationResolver" />
<property name="destinationName" value="jms/ClipEntryInQueue" />
<property name="messageListener" ref="myMessageListener" />
</bean>

<bean id="jndiDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="cache" value="true" />
</bean>

<bean id="myMessageListener" class="com.xyz.web.test.QueueListenerBean" />

</beans>


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

<web-app version="2.4" 
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Spring MDB Example</display-name>
  <!-- -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
 <!--  
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>file:/temp/applicationContext.xml</param-value>
</context-param>
  -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

1 comment:

Mr.Chowdary said...

Thanks for the example..
Very helpful..