Thursday, July 3, 2014

ACTIVE MQ WITH SPRING 2.5



How to intergrate active MQ with Spring and JAVA MX

Step 01

In this post I will show how to send JMS message to ActiveMQ from your Spring web application. I used Spring 2.5  web application that runs on Tomcat 6 running on JDK 6.

Frist download the Active MQ 5.2  
https://repository.apache.org/content/repositories/snapshots/org/apache/activemq/apache-activemq/5.4-SNAPSHOT/

 Copy the following jars to your WEB-INF/lib directory

  • activemq-all-5.4-SNAPSHOT.jar
  • activemq-pool-5.4-SNAPSHOT.jar
  •  jms.jar
Create a destination queue using active MQ web application

 http://127.0.0.1:8161/admin/
click on Queue and type a name  on

 Step 02

Create a bean which are going to send to the queue

public class MessageBean implements Serializable {

private String msgString;


private String getMsgString() {
 return  msgString;
}

private void setMsgString(String msgString) {
 this.msgString=msgString;
}

}

Create a MessageProducer interface and Implementation


public interface MessageProducer {
     public void send(Object message);
}

public class MessageProducerImpl implements MessageProducer {

    private static final Logger logger  = LoggerFactory.getLogger(MessageProducerImpl.class);

   
    private JmsTemplate jmsTemplate = null;
   
    private Queue queue;
   
    private TestMessageCreater messageCreator;
   
    @Override
    public void send(Object message) {
          this.messageCreator.setMessageBean((MessageBean )message);
          this.jmsTemplate.send(this.queue, this.messageCreator);
    }
   

    public Queue getQueue() {
        return queue;
    }


    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }


    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }


    public void setQueue(Queue queue) {
        this.queue = queue;
    }


    public MessageCreator getMessageCreator() {
        return messageCreator;
    }

    public void setMessageCreator(TestMessageCreater messageCreator) {
        this.messageCreator = messageCreator;
    }   
}

Create TestMessageCreater by implementing org.springframework.jms.core.MessageCreator interface



import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Session;

import org.springframework.jms.core.MessageCreator;

public class TestMessageCreater implements MessageCreator {

       private MessageBean messageBean;
   
       public MapMessage createMessage(Session session) throws JMSException {           
            MapMessage objectMsg = session.createMapMessage();           
            objectMsg.setString("processName", messageBean.getMsgString());                     
            return objectMsg;
        }
       
        public MessageBean getMessageBean() {
            return messageBean;
        }

        public void settMessageBean(MessageBean messageBean) {
            this.messageBean= messageBean;
        }
       
}

 Wiring througth Spring web application

JmsMessageProducer-context.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  
  
   
    <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg>
            <value>test.queue/value>
        </constructor-arg>
    </bean>
   
   
    <!-- Spring JMS Template -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
             <ref local="connectionFactory" />
         </property>
    </bean>
   
   
      <bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"  destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL">
                    <value>tcp://localhost:61616</value>
                </property>
            </bean>
        </property>
    </bean>
   
   
   
   
    <!-- a sample POJO message producer which uses a Spring JmsTemplate -->
    <bean id="messageProducer"     class="<your.packeage>MessageProducerImpl">
        <property name="jmsTemplate" ref="jmsTemplate"/>
        <property name="queue" ref="queue"/>
        <property name="messageCreator">
            <bean class="<your.packeage>.TestMessageCreator"/>
        </property>
    </bean>
   
    <bean name="messageService" class="<your.packeage>.MessageServiceImpl">
         <property name="messageProducer" ref="messageProducer" />
    </bean>
   
   
</beans>

Create a MessageService interace and MessageServiceImpl
this is used for to inject bean to your spring web action


 public interface MessageService {  
     public void sendMessage(MessageBean messageBean) ;      
}



public class MessageServiceImpl implements MessageService {

    private MessageProducer messageProducer;

    public void sendMessage(MessageBean messageBean) {
        messageProducer.send(messageBean);
    } 
   
   
     public MessageProducer getMessageProducer() {
        return messageProducer;
    }

    /**
     * @param messageProducer the messageProducer to set
     */
    public void setMessageProducer(MessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }

}


Inject messageService bean to your spring web action and try to send sample message


MessageBean messageBean = new MessageBean();
 messageBean.setMsgString("Hello Active MQ");
 messageService.sendMessage(messageBean);   

No comments:

Post a Comment