Spring and JMS Integration

To integrate spring with JMS, you need to create two applications.

  1. JMS Receiver Application
  2. JMS Sender Application

To create JMS application using spring, we are using Active MQ Server of Apache to create the Queue.

Let's see the simple steps to integration spring application with JMS:

Required Jar Files

1) You need to add spring core, spring misc,spring aop, spring j2ee and spring persistence core jar files.

download the all jar files for spring including aop, mvc, j2ee, remoting, oxm, etc.


2) Add activemqall5.9.jar file located inside the activemq directory.

Create a queue in ActiveMQ Server

Download the Active MQ Server Download Active MQ


Double Click on the activemq.bat file located inside apache-activemq-5.9.1-bin\apache-activemq-5.9.1\bin\win64 or win32 directory.

Now activemq server console will open.

Access the admin console of activemq server by http://localhost:8161/admin/ url.




Now, click on the Queues link, write myqueue in the textfield and click on the create button.




1) JMS Receiver Application

Let's see the simple steps to integration spring application with JMS:

  • MyMessageListener.java
  • TestListener.java
  • applicationContext.xml

1) MyMessageListener.java

package com.javahubpoint;  

import javax.jms.Message;  
import javax.jms.MessageListener;  
import javax.jms.TextMessage;  

public class MyMessageListener implements MessageListener{  

    @Override  

    public void onMessage(Message m) {  

        TextMessage message=(TextMessage)m;  

        try{  

            System.out.println(message.getText());  

        }catch (Exception e) {e.printStackTrace();  }  

    }  

}  

2) TestListener.java

package com.javahubpoint;  

import org.springframework.context.support.GenericXmlApplicationContext;  

public class TestListener {  

public static void main(String[] args) {  

    GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();  

    ctx.load("classpath:applicationContext.xml");  

    ctx.refresh(); 

    while(true){}  

}  

}  

3) applicationContext.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:jms="http://www.springframework.org/schema/jms"  

      

    xmlns:p="http://www.springframework.org/schema/p"  

    xsi:schemaLocation="http://www.springframework.org/schema/beans   

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

    http://www.springframework.org/schema/jms  

    http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">  

  

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"  

 p:brokerURL="tcp://localhost:61616" /> 

<bean id="listener" class="com.javahubpoint.MyMessageListener"></bean> 

<jms:listener-container container-type="default" connection-factory="connectionFactory"  

 acknowledge="auto">  

<jms:listener destination="myqueue" ref="listener" method="onMessage"></jms:listener>  

</jms:listener-container> 

</beans>  

2) JMS Sender Application

Let's see the files to create the JMS Sender application:

  • MyMessageSender.java
  • TestJmsSender.java
  • applicationContext.xml

1) MyMessageListener.java

package com.javahubpoint;  

import javax.jms.*;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.jms.core.JmsTemplate;  
import org.springframework.jms.core.MessageCreator;  
import org.springframework.stereotype.Component; 

@Component("messageSender")  

public class MyMessageSender {  

@Autowired  

private JmsTemplate jmsTemplate;  

public void sendMessage(final String message){  

    jmsTemplate.send(new MessageCreator(){ 

        @Override  

        public Message createMessage(Session session) throws JMSException {  

            return session.createTextMessage(message);  

        }  

    });  

}  

}  

2) TestJmsSender.java

package com.javahubpoint;  

import org.springframework.context.support.GenericXmlApplicationContext;  

public class TestJmsSender {  

public static void main(String[] args) {  

    GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();  

    ctx.load("classpath:applicationContext.xml");  

    ctx.refresh(); 


    MyMessageSender sender=ctx.getBean("messageSender",MyMessageSender.class);  

    sender.sendMessage("hello jms3");  

      

}  

}  

3) applicationContext.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:context="http://www.springframework.org/schema/context"  

    xmlns:jms="http://www.springframework.org/schema/jms"  

      

    xmlns:p="http://www.springframework.org/schema/p"  

    xsi:schemaLocation="http://www.springframework.org/schema/beans   

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

    http://www.springframework.org/schema/context  

    http://www.springframework.org/schema/context/spring-context-3.0.xsd  

    http://www.springframework.org/schema/jms  

    http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">  

  

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"  

 p:brokerURL="tcp://localhost:61616" /> 

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  

<constructor-arg name="connectionFactory" ref="connectionFactory"></constructor-arg>  

<property name="defaultDestinationName" value="myqueue"></property>  

</bean> 

<context:component-scan base-package="com.javahubpoint"></context:component-scan> 

</beans>  

download JMS Receiver example (developed using Myeclipse IDE)

download JMS Sender example(developed using Myeclipse IDE)