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.
http://localhost:8161/admin/queues.jsp

There should be no visible queues.
Create a Maven Project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.skills421.camel.basics</groupId> <artifactId>CamelBlog</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- Needed for ActiveMQ - JMS --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.3.2</version> </dependency> <!-- JUnit for testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
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); connection.start(); ObjectMessage m1 = session.createObjectMessage(); m1.setObject(message); producer.send(m1); connection.close(); } }
Create the Person Object
package com.skills421.examples.camel.basics; import java.io.Serializable; public class Person implements Serializable { /** * */ private static final long serialVersionUID = -2749977649306134186L; private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override 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; @Before public void setup() { jmsManager = new JMSManager(); } @Test public void testSendSimpleObject() { try { Person p = new Person("Jon Doe", 21); jmsManager.sendMessage(p, "example1-queue"); } catch (Exception e) { fail(e.getMessage()); e.printStackTrace(); } } }
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.
Notes
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
andTemporaryQueues
. - It provides a way to create
Queue
orTopic
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 transactedacknowledgeMode
– indicates whether the consumer or the client will acknowledge any messages it receives; ignored if the session is transacted. Legal values areSession.AUTO_ACKNOWLEDGE
,Session.CLIENT_ACKNOWLEDGE
, andSession.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.
Thanks for the post. Saved me some time.
In JMSManager#sendMessage, the queueName is never used. Instead, the queueName is hardcoded to “example-queue”.