Implementing JMS with Spring: Message Driven POJO
The previous post described how to implement a JMS messaging client using Spring JMS. This post will describe how to implement the Message listener as a spring Message driven POJO. Follow these steps to implement the Message driven POJO
  1. Create the Message Driven POJO: The only requirement for the Message Driven POJO is to implement the MessageListener interface. The following listing shows the code for the MDP
    public class SpringMDP implements MessageListener {
    public void onMessage(Message message) {
    try {
    System.out.println(((TextMessage) message).getText());
    } catch (JMSException ex) {
    throw new RuntimeException(ex);
    }
    }
    }
    SpringMDP.java

  2. Create the bean definition in applicationContext.xml file.
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <!-- this is the Message Driven POJO (MDP) -->
    <bean id="messageListener" class="jms.SpringMDP" />

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
    <props>
    <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
    <prop key="java.naming.provider.url">t3://localhost:20001</prop>
    </props>
    </property>
    </bean>
    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
    <ref bean="jndiTemplate" />
    </property>
    <property name="jndiName">
    <value>jms/connectionFactory</value>
    </property>
    </bean>

    <bean id="queue" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
    <ref bean="jndiTemplate" />
    </property>
    <property name="jndiName">
    <value>jms/testQueue</value>
    </property>
    </bean>


    <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="concurrentConsumers" value="5" />
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="queue" />
    <property name="messageListener" ref="messageListener" />
    </bean>
    </beans>
    WEB-INF/applicationContext.xml

    The Message listener container handles all the required functions for making the Simple POJO a Message Driven POJO.

  3. Update Web.xml to include a listener for spring.
    <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

3 comments:

Anonymous said...

I am getting this error. Any Ideas.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in ServletContext resource [/WEB-INF/applicationContext-spring.xml]: Invocation of init method failed; nested exception is org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 203 completed: No
Caused by:
org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 203 completed: No
at com.sun.corba.se.impl.logging.ORBUtilSystemException.writeErrorSend(Unknown Source)

Wes said...

Hi, I wrote a MDB, deployed it to the weblogic server ( I am not using web) .

I used your code, and was able to publish successfully, even without using your applicationcontext.xml. Somehow it is reading from the weblogic-ejb-jar.xml instead. How can I make it such that it is reading from your application context xml file? Please advise...

Anonymous said...

Any idea how to start/stop listner if something goes wrong without having to restart the server.

In Webpshere we can access the listner incase something went wrong and start the listner can something we configure something in spring that can access the listner to help admins