Send a JMS message using Spring Framework

I was completing a presentation to demonstrate the use of Spring JMS integration but I did not find any sample application or any sample documentation to do that. So I decided to write a sample application which will send a message to a JMS Server.

So I got ActiveMQ 5.0, Jetty 6.1.5, Spring 2.0.6 set up in my computer and started my development.

First thing I needed was a service interface and its implementation. Below are the classes.

public interface MyServiceSendMsg {

void simpleSend(String message);


The implementation looks like this:

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Destination;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.JmsTemplate;

public class MyServiceSendMsgImpl implements MyServiceSendMsg {
private JmsTemplate jmsTemplate;
private Destination destination;

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;

public void setDestination(Destination destination) {
this.destination = destination;

public void simpleSend(final String strmsg) {
this.jmsTemplate.send(this.destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(strmsg);

In the service implementation, I am injecting the JmsTemplate and the Destination and using the send method of the JmsTemplate to send the message to the destination.

Below is the spring configuration:

<bean id=”myServiceSendMsg ” class=”MyServiceSendMsgImpl “>
<property name=”jmsTemplate”>
<ref bean=”myJmsTemplate”></ref>
<property name=”destination”>
<ref bean=”destination” />

In the above the configuration, jmsTemplate is referencing myJmsTemplate.

<bean id=”myJmsTemplate” class=”org.springframework.jms.core.JmsTemplate”>
<property name=”connectionFactory”>
<bean class=”org.springframework.jms.connection.SingleConnectionFactory”>
<property name=”targetConnectionFactory”>
<ref local=”jmsFactory” />

You will notice jmsFactory is configured using Spring’s namespace.

<amq:connectionFactory id=”jmsFactory” brokerURL=”tcp://localhost:61616″/>

To enable usage of namespace, Spring schema based configuration is used as shown below. You will notice there is a reference to the activemq core xsd also here.


In addition to the configuration, I had to add xbean-spring-3.2-SNAPSHOT.jar file also. Below is a screenshot of the lib folder. Ofcourse, you probably don’t need most of it but I had used it in my application.

jar files

Below is the configuraton for the destination.

<amq:queue id=”destination” physicalName=”test”/>

Now we have the complete configuration to send a message to a queue called test to JMS server at localhost.

Hope this sample configuration and sample helps you in using Spring’s JmsTemplate to send message to an ActiveMQ server.

If you have any issues, question, please forward to jsmammen at gmail dot com

    Leave a Reply

    Fill in your details below or click an icon to log in: Logo

    You are commenting using your account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

  • January 2008
    M T W T F S S

%d bloggers like this: