Searching...
Saturday, 14 December 2013

Spring JmsTemplate and MessageListener Example

21:56

Spring provides a JMS integration framework that simplifies the use of the JMS API, the JmsTemplate class is the core class which is available in spring JMS package. This class simplifies sending and receiving messages via simple spring configuration. In this example we will see how to send and listen a message using spring JmsTemplate and Apache activemq.

Environment Used

Java 1.7
Spring-jms 3.1.2
Activemq 5.8.0
Maven

Prerequisite

Start ActiveMQ message broker.

1. Create a Maven project and add below dependencies

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.4.2</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>

2. Create Spring configuration file (applicationContext.xml)

Configure Activmq ,JMS Message listener and jms template beans here

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jms="http://www.springframework.org/schema/jms"
    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://activemq.apache.org/schema/core
                          http://activemq.apache.org/schema/core/activemq-core.xsd">

 
    <context:component-scan base-package="com.pretech" />
    <context:annotation-config />
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL">
            <value>tcp://localhost:61616</value>
        </property>
    </bean>
 
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="myMessageQueue" />
    </bean>
    <jms:listener-container connection-factory="connectionFactory">
        <jms:listener destination="myMessageQueue" ref="messageListenerExample" />
    </jms:listener-container>
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="destination" />
    </bean>
 
</beans>

3. Create a Listener class to implements MessageListener

package com.pretech;
 
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
 
import org.springframework.stereotype.Component;
 
@Component("messageListenerExample")
public class MessageListenerExample implements MessageListener {
 
    public void onMessage(Message message) {
        if (message instanceof ObjectMessage) {
            ObjectMessage msg = (ObjectMessage)message;
          try {
            System.out.println("OnMessage Received :"+msg.getObject().toString());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }}
}
 
}

4. Create a Main class to send messages (MessageSender.java)

package com.pretech;
 
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
 
public class MessageSender {
    public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
          JmsTemplate jmsTemplate=(JmsTemplate) context.getBean("jmsTemplate");
          jmsTemplate.send(
                new MessageCreator() {
                      public ObjectMessage  createMessage(Session session) throws JMSException {
                          ObjectMessage message = session.createObjectMessage();
                          message.setObject("My first Message");                      
                           return message;
                      }
        }  );
 
          System.out.println("MESSAGE SENT TO myMessageQueue");
    }
 
}

5. Run it

Run the MessageSender.java and we will see below output in the console 

OnMessage Received  :My first Message
MESSAGE SENT TO myMessageQueue

Reference

Spring JMS

0 comments: