Home » Java » Enterprise Java » JMS Sender application with ActiveMQ and Maven

About Abhimanyu Prasad

Abhimanyu Prasad
Abhimanyu is a passionate tech blogger and senior programmer, who has an extensive end-to-end development experience with wide range of technologies. He is the founder and administrator at jCombat.

JMS Sender application with ActiveMQ and Maven

We have already seen how to create a JMS Receiver application with ActiveMQ and Maven. Let’s check out how we can similarly create the JMS Sender application.

web.xml remains the same as we had used for creating the receiver application:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

jmsContext.xml would change a bit to something like:

<?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"
	<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616" />
	<!-- <jms:listener-container container-type="default"
		connection-factory="connectionFactory" acknowledge="auto">
		<jms:listener destination="testQueue" ref="simpleMessageListener"
			method="onMessage" />
	</jms:listener-container> -->
	<!-- To send JMS message -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="defaultDestinationName" value="testQueue"/>
	<bean id="simpleMessageSender" class="com.jcombat.sender.SimpleMessageSender">
		<property name="jmsTemplate" ref="jmsTemplate" />

Note that we have added two new beans for jmsTemplate and messageSender class, which will be using the jmsTemplate instance to send message to the destination queue.

Now let’s create a message sender class SimpleMessageSender.java, which will actually be sending the message:

package com.jcombat.sender;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class SimpleMessageSender {
	private JmsTemplate jmsTemplate;
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	public void sendMessage(final String message){  
	    jmsTemplate.send(new MessageCreator(){  
	        public Message createMessage(Session session) throws JMSException {  
	            return session.createTextMessage(message);  

Almost done. Now let’s create the client class TestClient.java, which will get the simpleMessageSender bean from jmsContext and send some message string by calling it’s sendMessage method, thus dropping the message to the configured queue testQueue (as seen highlighted in the jmsContext file content shown above).

package com.jcombat.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.jcombat.sender.SimpleMessageSender;
public class TestClient {
	public static void main(String[] args) throws Exception {
		ApplicationContext jmsContext = null;
		jmsContext = new FileSystemXmlApplicationContext(
		SimpleMessageSender messageSender = (SimpleMessageSender) jmsContext
		// Create a session within the connection.

Now run the TestClient class as Java Application.

Verify message on the destination queue

To verify that the message that we have sent through our client class has successfully arrived at the destination queue i.e. testQueue, login to the Hawtio console and from the queues listed on the left, click on the testQueue link, which shows up the testQueue details on the main content area as can be seen below:


Now click on the Message that could be seen listed under the ‘Browse’ tab. We could see the same message that we had sent pops out as:


Reference: JMS Sender application with ActiveMQ and Maven from our JCG partner Abhimanyu Prasad at the jCombat blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of