Tuesday, November 10, 2009

Spring JMS message consumer ( JBoss MQ )

  1. Create a new web project in eclipse.
  2. Under the WEB-INF/lib folder copy the following jar files
    1. commons-logging.jar
    2. spring.jar
    3. spring-jms.jar
  3. Create a listener bean
  4. package com.test.jms.pojo;
    
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.TextMessage;
    
    public class Listener1 implements MessageListener {
    
     public void onMessage(Message message) {
      if (message instanceof TextMessage) {
       try {
        System.out.println(((TextMessage) message).getText());
       } catch (JMSException ex) {
        throw new RuntimeException(ex);
       }
      } else {
       throw new IllegalArgumentException(
         "Message must be of type TextMessage");
      }
    
     }
    
    }
  5. Create Spring configuration file.
  6. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.springframework.org/schema/beans"
     xsi:schemalocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     <bean class="com.test.jms.pojo.Listener1" id="listener1">
      <bean
       class="org.springframework.jms.listener.DefaultMessageListenerContainer"
       id="jmsContainer">
       <property name="connectionFactory" ref="connectionFactory"></property>
       <property name="destination" ref="c8queue"></property>
       <property name="messageListener" ref="listener1"></property>
      </bean>
      <bean class="org.springframework.jms.core.JmsTemplate" id="jmsTemplate">
       <property name="connectionFactory" ref="connectionFactory"></property>
      </bean>
      <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="c8queue">
       <property name="jndiName" value="queue/c8queue"></property>
       <property name="jndiTemplate" ref="jndiTemplate"></property>
       <property name="resourceRef" value="false"></property>
      </bean>
      <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="connectionFactory">
       <property name="jndiName" value="ConnectionFactory"></property>
       <property name="jndiTemplate" ref="jndiTemplate"></property>
       <property name="resourceRef" value="false"></property>
      </bean>
      <bean class="org.springframework.jndi.JndiTemplate" id="jndiTemplate">
       <property name="environment">
        <props>
         <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory </prop>
         <prop key="java.naming.provider.url">jnp://localhost:1099</prop>
         <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces </prop>
        </props>
       </property>
      </bean>
     </bean>
    
  7. Create queue in Jboss
    1. In folder $JBOSS_HOME/server/default/deploy/jms create a file yourProj-service.xml
    2. <server>
       <mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=c8queue">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager </depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager </depends>
        <attribute name="MessageCounterHistoryDayLimit">-1</attribute>
        <attribute name="SecurityConf">
         <security>
          <role name="guest" read="true" write="true">
           <role create="false" name="publisher" read="true" write="true">
            <role create="false" name="noacc" read="false" write="false"></role>
           </role>
          </role>
         </security>
        </attribute>
       </mbean>
      </server>
      
  8. Edit web.xml to load spring configuration
  9. <web-app id="WebApp_ID" version="2.5"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <display-name>tstSprJms</display-name>
     <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
     <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>
    </web-app>
    
  10. Restart the server

    No comments: