Send JMS Object Message to ActiveMQ

In this post we are going to send a simple object message to ActiveMQ using JMS.

Install ActiveMQ

First install ActiveMQ as per my previous post.

Start ActiveMQ as per the instructions in the previous

activemq restart

Open the web browser and view the queues.



There should be no visible queues.

Create a Maven Project

<project xmlns="" xmlns:xsi=""


		<!-- Needed for ActiveMQ - JMS -->

		<!-- JUnit for testing -->


Create the JMSManager

package com.skills421.examples.camel.basics;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSManager
	public void sendMessage(final Serializable message, final String queueName) throws Exception
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
		Connection connection = connectionFactory.createConnection();
		Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

		Queue queue = session.createQueue("example-queue");
		MessageProducer producer = session.createProducer(queue);
		ObjectMessage m1 = session.createObjectMessage();	

Create the Person Object

package com.skills421.examples.camel.basics;


public class Person implements Serializable
	private static final long serialVersionUID = -2749977649306134186L;
	private String name;
	private int age;

	public Person(String name, int age)
		super(); = name;
		this.age = age;

	public String getName()
		return name;

	public void setName(String name)
	{ = name;

	public int getAge()
		return age;

	public void setAge(int age)
		this.age = age;

	public String toString()
		return String.format("Person [name=%s, age=%s]", name, age);


Create the JUnit Test

package com.skills421.examples.camel.basics;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class JMSManagerTest
	private JMSManager jmsManager;

	public void setup()
		jmsManager = new JMSManager();

	public void testSendSimpleObject()
			Person p = new Person("Jon Doe", 21);
			jmsManager.sendMessage(p, "example1-queue");
		catch (Exception e)


Run the JUnit Test

Refresh the ActiveMQ Web Page


Note that we can now see the example-queue and it contains one message

Click on the message and we can see the message details


If you look at the bottom of the screen you can see the message content is a Person object, even if the web page complains that it doesn’t know how to process a Person.


The ActiveMQConnection.DEFAULT_BROKER_URL is tcp://localhost:61616 and references the same ActiveMQConnection session we are looking at in the web browser.

The session serves many purposes including:

  • It is a factory for its message producers and consumers.
  • It supplies provider-optimized message factories.
  • It is a factory for TemporaryTopics and TemporaryQueues.
  • It provides a way to create Queue or Topic objects for those clients that need to dynamically manipulate provider-specific destination names.
  • It supports a single series of transactions that combine work spanning its producers and consumers into atomic units.
  • It defines a serial order for the messages it consumes and the messages it produces.
  • It retains messages it consumes until they have been acknowledged.
  • It serializes execution of message listeners registered with its message consumers.
  • It is a factory for QueueBrowsers.

When we create the session, we specify the following parameters:

  • transacted – indicates whether the session is transacted
  • acknowledgeMode – indicates whether the consumer or the client will acknowledge any messages it receives; ignored if the session is transacted. Legal values areSession.AUTO_ACKNOWLEDGESession.CLIENT_ACKNOWLEDGE, and Session.DUPS_OK_ACKNOWLEDGE.

We then create a queue / a handle to the queue if it already exists

To send messages onto the queue, we need to create a MessageProducer, and to retrieve messages from the queue we need to create a MessageConsumer.  In this example, we have only created a producer as we want to use the web interface to view the messages.

Before we can send or receive messages, we must first start the connection.

We use the session to create the message object and then set the values of the message before sending it.

Finally we close our connection.


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