Pages

Monday, October 21, 2013

Step by step guide to create a Spring Message Driven Pojo - Spring MDP

In your spring application you may have a requirement to listen to the incoming messages. This can be easily achieved by Spring MDP (Spring Message Driven Pojo). When you are doing for the first time, you will face few issues and it will take some time. I went through that problem so thought of documenting it.

Create a sample message listener class which implements the MessageListener interface. This interface has the onMessage method which has to be implemented by the class. Here it is just simply prints a message to the console.

import javax.jms.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SampleMessageListener implements MessageListener {
 private static final Logger logger = LoggerFactory
   .getLogger(SampleMessageListener .class);
 @Autowired
 MessageHandler Handler;
 public void onMessage(Message message) {

  logger.info("Message received ..");
  try {
System.out.println("Message received..");
  } catch (Exception ccEx) {
   logger.error("Processing  failed :" + ccEx);
   throw new RuntimeException(ccEx);
  }
 }
}

Next the spring 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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jms="http://www.springframework.org/schema/jms" 
 xmlns:jee="http://www.springframework.org/schema/jee" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.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.xsd
                           http://www.springframework.org/schema/jee
                           http://www.springframework.org/schema/jee/spring-jee-3.0.xsd>

 <context:component-scan base-package="com.ravi.jms" />
 <context:annotation-config />

 <!-- JMS config -->
 <bean id="sampleMessageListener" class="com.ravi.jms.SampleMessageListener" />
 <jee:jndi-lookup id="sampleConnectionFactory" resource-ref="true" jndi-name="jms/Sample_CF" />
 <jee:jndi-lookup id="sampleQueueName" resource-ref="true" jndi-name="jms/sampleIncomingQ" />

 <bean id="sampleMessageListenerContainer"
  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  <property name="connectionFactory" ref="sampleConnectionFactory" />
  <property name="destination" ref="jms_acknack_queueName" />
  <property name="messageListener" ref="recievedAckNackMessageListener" />
  <property name="sessionTransacted"  value="true"></property>
  <property name="recoveryInterval" value="300000"></property>
 </bean>
</beans>

Web.xml of the application. The resources (Queue connection factory and queue) has to be defined here.

<web-app id="WebApp_ID" 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>Sample Application</display-name>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>MyDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>MyDispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <resource-ref id="ResourceRef_001">
  <description>The QueueConnectionFactory for the service</description>
  <res-ref-name>jms/Sample_CF</res-ref-name>
  <res-type>javax.jms.QueueConnectionFactory</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>
 
 <resource-ref id="ResourceRef_002">
  <description>The Queue for the service</description>
  <res-ref-name>jms/sampleIncomingQ</res-ref-name>
  <res-type>javax.jms.Queue</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>
</web-app>


If you are deploying in websphere you need the ibm-bnd.xmi file also.

<?xml version="1.0" encoding="UTF-8"?>
<webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1238927070062"
virtualHostName="default_host">

  <webapp href="WEB-INF/web.xml#WebApp_ID"/>

  <resRefBindings xmi:id="ResourceRefBinding_001" jndiName="jms/Sample_CF">
    <bindingResourceRef href="WEB-INF/web.xml#ResourceRef_001"/>
  </resRefBindings>

  <resRefBindings xmi:id="ResourceRefBinding_002" jndiName="jms/sampleIncomingQ">
    <bindingResourceRef href="WEB-INF/web.xml#ResourceRef_002"/>
  </resRefBindings>  
</webappbnd:WebAppBinding>

No comments:

Post a Comment

 
Blogger Templates