In this blog i will show you how to use Spring JmsTemplate with ActiveMQ as Message Broker.
You need to have the following jars in your project lib folder
1. activemq-core-5.4.3.jar ( you can download the Active MQ distribtion from this link. you can find this activemq-core jar in lib folder after extracting the downloaded zip)
2. Javaee-api.jar (download this jar from this link)
3. spring 3 jars( which you can download from springsource site)
I will be using NetBeans7.1 latest relase .
Step1. Create a new java project
2. Right click on project node and select properties and add the above mentioned jars to library(activemq-core.jar,javaee-api.jar and spring jars).
If you are using netbeans IDE you can add spring jars by selecting Add Library button and select the spring jar in the list.
3.Create a class Called JMSService which contains send( ) and receive( ) methods.
/*
This method is used for sending messages to the destination( in this case this send message to Queue)
*/
public void sendMessage(){
jmsTemplate.send( new MessageCreator(){
public Message createMessage(Session session) throws JMSException{
return session.createTextMessage("Hello");
}
}
);
}
/*
.This receives message from the queue
*/
public String receiveMessage(){
try {
TextMessage receivedMessage=(TextMessage) jmsTemplate.receive();
return(String)receivedMessage.getText();
} catch(JMSException jmsException){
throw JmsUtils.convertJmsAccessException(jmsException);
}
}
JMSService.java
-----------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.msn.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.support.JmsUtils;
public class JMSService {
private JmsTemplate jmsTemplate;
public JMSSender(JmsTemplate template){
this.jmsTemplate=template;
}
public void sendMessage(){
jmsTemplate.send( new MessageCreator(){
public Message createMessage(Session session) throws JMSException{
return session.createTextMessage("Hello");
}
}
);
}
public String receiveMessage(){
try {
TextMessage receivedMessage=(TextMessage) jmsTemplate.receive();
return(String)receivedMessage.getText();
} catch(JMSException jmsException){
throw JmsUtils.convertJmsAccessException(jmsException);
}
}
}
4. Configure ConnectionFactory,Queue(destination), JmsTemplate and JMSService as beans in the spring configuration file
jmscontext.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name ="brokerURL" value="tcp://localhost:61616"/>
</bean>
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue1"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="queue1"/>
</bean>
<bean id="messageService" class="com.msn.jms.JMSService" >
<constructor-arg ref="jmsTemplate"/>
</bean>
</beans>
5. Create a Test class with main method which tests JMSService send( ) and receive( ) methods.
JMSClient.java
-----------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.msn.jms;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author snaidu26
*/
public class MessageClient {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("com/msn/jms/jmscontext.xml");
JMSSender sender=(JMSSender) ctx.getBean("messageService");
sender.sendMessage();
System.out.println("Message sent to server");
System.out.println("Message Received: "+sender.receiveMessage());
}
}
6. Project Structure looks like :
7.When we run the MessageClient
You need to have the following jars in your project lib folder
1. activemq-core-5.4.3.jar ( you can download the Active MQ distribtion from this link. you can find this activemq-core jar in lib folder after extracting the downloaded zip)
2. Javaee-api.jar (download this jar from this link)
3. spring 3 jars( which you can download from springsource site)
I will be using NetBeans7.1 latest relase .
Step1. Create a new java project
2. Right click on project node and select properties and add the above mentioned jars to library(activemq-core.jar,javaee-api.jar and spring jars).
If you are using netbeans IDE you can add spring jars by selecting Add Library button and select the spring jar in the list.
3.Create a class Called JMSService which contains send( ) and receive( ) methods.
/*
This method is used for sending messages to the destination( in this case this send message to Queue)
*/
public void sendMessage(){
jmsTemplate.send( new MessageCreator(){
public Message createMessage(Session session) throws JMSException{
return session.createTextMessage("Hello");
}
}
);
}
/*
.This receives message from the queue
*/
public String receiveMessage(){
try {
TextMessage receivedMessage=(TextMessage) jmsTemplate.receive();
return(String)receivedMessage.getText();
} catch(JMSException jmsException){
throw JmsUtils.convertJmsAccessException(jmsException);
}
}
JMSService.java
-----------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.msn.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.support.JmsUtils;
public class JMSService {
private JmsTemplate jmsTemplate;
public JMSSender(JmsTemplate template){
this.jmsTemplate=template;
}
public void sendMessage(){
jmsTemplate.send( new MessageCreator(){
public Message createMessage(Session session) throws JMSException{
return session.createTextMessage("Hello");
}
}
);
}
public String receiveMessage(){
try {
TextMessage receivedMessage=(TextMessage) jmsTemplate.receive();
return(String)receivedMessage.getText();
} catch(JMSException jmsException){
throw JmsUtils.convertJmsAccessException(jmsException);
}
}
}
4. Configure ConnectionFactory,Queue(destination), JmsTemplate and JMSService as beans in the spring configuration file
jmscontext.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name ="brokerURL" value="tcp://localhost:61616"/>
</bean>
<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue1"/>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="queue1"/>
</bean>
<bean id="messageService" class="com.msn.jms.JMSService" >
<constructor-arg ref="jmsTemplate"/>
</bean>
</beans>
5. Create a Test class with main method which tests JMSService send( ) and receive( ) methods.
JMSClient.java
-----------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.msn.jms;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author snaidu26
*/
public class MessageClient {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("com/msn/jms/jmscontext.xml");
JMSSender sender=(JMSSender) ctx.getBean("messageService");
sender.sendMessage();
System.out.println("Message sent to server");
System.out.println("Message Received: "+sender.receiveMessage());
}
}
6. Project Structure looks like :
7.When we run the MessageClient
No comments:
Post a Comment