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 receive a message to message broker 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)<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>
Configure Activmq 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">
<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>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="destination" />
</bean>
</beans>
<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">
<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>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="destination" />
</bean>
</beans>
3. Create a Main class to send and receive messages (MessageReceiver.java)
package com.pretech;
import javax.jms.JMSException;
import javax.jms.Message;
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 MessageReceiver {
public static void main(String[] args) throws JMSException {
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");
Message receivedMessage=jmsTemplate.receive("myMessageQueue");
ObjectMessage msg = (ObjectMessage)receivedMessage;
System.out.println("Message Received :"+msg.getObject().toString());
}
}
import javax.jms.JMSException;
import javax.jms.Message;
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 MessageReceiver {
public static void main(String[] args) throws JMSException {
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");
Message receivedMessage=jmsTemplate.receive("myMessageQueue");
ObjectMessage msg = (ObjectMessage)receivedMessage;
System.out.println("Message Received :"+msg.getObject().toString());
}
}
4. Run itRun the MessageSender.java and we will see below output in the console and Active MQ broker
No comments:
Post a Comment