Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring with JMS


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.

activemq1

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

activemq2

1) JMS Receiver Application

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

  1. MyMessageListener.java
  2. TestListener.java
  3. applicationContext.xml

1) MyMessageListener.java

  1. package com.Kreationnext;
  2. import javax.jms.Message;
  3. import javax.jms.MessageListener;
  4. import javax.jms.TextMessage;
  5. public class MyMessageListener implements MessageListener{
  6.     @Override
  7.     public void onMessage(Message m) {
  8.         TextMessage message=(TextMessage)m;
  9.         try{
  10.             System.out.println(message.getText());
  11.         }catch (Exception e) {e.printStackTrace();  }
  12.     }
  13. }

2) TestListener.java

  1. package com.Kreationnext;
  2. import org.springframework.context.support.GenericXmlApplicationContext;
  3. public class TestListener {
  4. public static void main(String[] args) {
  5.     GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();
  6.     ctx.load(“classpath:applicationContext.xml”);
  7.     ctx.refresh();
  8.     while(true){}
  9. }
  10. }

3) applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“http://www.springframework.org/schema/beans”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xmlns:jms=“http://www.springframework.org/schema/jms”
  6.     xmlns:p=“http://www.springframework.org/schema/p”
  7.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  8.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9.     http://www.springframework.org/schema/jms
  10.     http://www.springframework.org/schema/jms/spring-jms-3.0.xsd”>
  11. <bean id=“connectionFactory” class=“org.apache.activemq.ActiveMQConnectionFactory”
  12.  p:brokerURL=“tcp://localhost:61616” />
  13. <bean id=“listener” class=“com.Kreationnext.MyMessageListener”></bean>
  14. <jms:listener-container container-type=“default” connection-factory=“connectionFactory”
  15.  acknowledge=“auto”>
  16. <jms:listener destination=“myqueue” ref=“listener” method=“onMessage”></jms:listener>
  17. </jms:listener-container>
  18. </beans>

2) JMS Sender Application

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

  1. MyMessageSender.java
  2. TestJmsSender.java
  3. applicationContext.xml

1) MyMessageListener.java

  1. package com.Kreationnext;
  2. import javax.jms.*;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.jms.core.JmsTemplate;
  5. import org.springframework.jms.core.MessageCreator;
  6. import org.springframework.stereotype.Component;
  7. @Component(“messageSender”)
  8. public class MyMessageSender {
  9. @Autowired
  10. private JmsTemplate jmsTemplate;
  11. public void sendMessage(final String message){
  12.     jmsTemplate.send(new MessageCreator(){
  13.         @Override
  14.         public Message createMessage(Session session) throws JMSException {
  15.             return session.createTextMessage(message);
  16.         }
  17.     });
  18. }
  19. }

2) TestJmsSender.java

  1. package com.Kreationnext;
  2. import org.springframework.context.support.GenericXmlApplicationContext;
  3. public class TestJmsSender {
  4. public static void main(String[] args) {
  5.     GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();
  6.     ctx.load(“classpath:applicationContext.xml”);
  7.     ctx.refresh();
  8.     MyMessageSender sender=ctx.getBean(“messageSender”,MyMessageSender.class);
  9.     sender.sendMessage(“hello jms3”);
  10. }
  11. }

3) applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“http://www.springframework.org/schema/beans”
  4.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.     xmlns:context=“http://www.springframework.org/schema/context”
  6.     xmlns:jms=“http://www.springframework.org/schema/jms”
  7.     xmlns:p=“http://www.springframework.org/schema/p”
  8.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  9.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10.     http://www.springframework.org/schema/context
  11.     http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12.     http://www.springframework.org/schema/jms
  13.     http://www.springframework.org/schema/jms/spring-jms-3.0.xsd”>
  14. <bean id=“connectionFactory” class=“org.apache.activemq.ActiveMQConnectionFactory”
  15.  p:brokerURL=“tcp://localhost:61616” />
  16. <bean id=“jmsTemplate” class=“org.springframework.jms.core.JmsTemplate”>
  17. <constructor-arg name=“connectionFactory” ref=“connectionFactory”></constructor-arg>
  18. <property name=“defaultDestinationName” value=“myqueue”></property>
  19. </bean>
  20. <context:component-scan base-package=“com.Kreationnext></context:component-scan>
  21. </beans>

Comments

comments