Other Categories

Simple JMS 1.1 Producer and Consumer Example With Eclipse and Embedded JBoss HornetQ Server

Before going to through this post, please read my previous post at “JMS API 1.1 Producer and Consumer” to understand some baby steps to develop JMS 1.1 Producer and Consumer programs.

In this post, we are going to develop a Simple JMS 1.1 Producer and Consumer Example With Eclipse IDE and Embedded JBoss HornetQ Server.

Post Brief TOC

  • Introduction
  • Steps to Develop a JMS V.1.1 Example
  • Final Project Structure
  • Execute and Observe the Output


As a Novice Developer to JMS API, it is bit tough to understand about JMS Servers, ConnectionFactory Creation, Queue Creation, Topic Creation etc. We will discuss these concepts in detail in coming posts. First of all, we should understand how to write a simple JMS Producer and Consumer programs without much effort.

To develop this example, we are going to use Eclipse IDE, JMS 1.1 API, Maven and JBoss Embedded HornetQ JMS Server. It is very easy to configure ConnectionFactory, Queue, Topic etc. with this embedded server. We just need to configure some XML files.

To use Embedded JMS Server, we don’t need to download and install any software. We just need to configure some Jars in our Maven Project. Let’s start developing application now.

Steps to Develop a JMS V.1.1 Example:

<project xmlns="" 

NOTE:-This pom.xml contains 3 dependencies

  1. hornetq-core-x.x.jar:- It contains to provide base API for both JMS server and client programs.
  2. hornetq-jms-server-x.x.jar:- It contains API to provide Embedded JBoss HornetQ Server.
  3. hornetq-core-x.x.jar:- It contains JMS API implementation for JMS Session,Queue,Topic etc.
  • Configure one user in “hornetq-users.xml”t o run this program.
  • hornetq-users.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration xmlns="urn:hornetq" 
    xsi:schemaLocation="urn:hornetq /schema/hornetq-users.xsd">
        <defaultuser name="jduser" password="jduser">
            <role name="jduser"/>

    Here we have defined one user “jduser” to use them in HornetQ Configuration XML file to configure Security.

  • Configure in-memory server configuration “hornetq-configuration.xml” to run this program. It is used to configure In-Memory JMS Server.
  • hornetq-configuration.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration xmlns="urn:hornetq" xmlns:xsi="" xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd">
            <connector name="in-vm">
            <acceptor name="in-vm">
            <security-setting match="">
                <permission type="consume" roles="jduser"/>
                <permission type="send" roles="jduser"/>

    Here We have configured JBoss HornetQ In-Memory Server and Security details.

  • Configure in-memory server ConnectionFactory and Queue “hornetq-jms.xml” to run this program. It is used to configure JMS Settings.
  • hornetq-jms.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration xmlns="urn:hornetq" 
    xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd">
        <connection-factory name="JDConnectionFactory">
                <connector-ref connector-name="in-vm"/>
                <entry name="JDConnectionFactory"/>
        <queue name="JDQueue">
            <entry name="/queue/JDQueue"/>

    Here we have configured ConnectionFactory with “JDConnectionFactory” name and Queue destination with “/queue/JDQueue” JNDI name. We have linked these two things to previous configured JBoss HornetQ In-Memory Server.

  • Create a Simple Java class “EmbeddedHornetQJMSExample” and copy below sample code.
    package com.jd.embedded.jms.hornetq;
    import javax.jms.*;
    import org.hornetq.jms.server.embedded.EmbeddedJMS;
    public class EmbeddedHornetQJMSExample
       public static void main(final String[] args)
             // This EmbeddedJMS class acts as a JMSServer for example: JBoss HornetQ Server
             EmbeddedJMS jmsServer = new EmbeddedJMS();
             System.out.println("JD: Embedded JMS Server started.");
             ConnectionFactory connectionFactory = 
             Queue queue = (Queue)jmsServer.lookup("/queue/JDQueue");
             Connection connection = null;
                connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                //1. Producer Code Start
                MessageProducer producer = session.createProducer(queue);
                TextMessage message = session.createTextMessage("Hello, I'm from JournalDev(JD).");
                System.out.println("Sending message: " + message.getText());
                //2. Consumer Code Start
                MessageConsumer messageConsumer = session.createConsumer(queue);
                TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
                System.out.println("Received message: " + messageReceived.getText());
                if (connection != null)
                System.out.println("JD: Embedded JMS Server stopped.");
          catch (Exception e)

    Just for simplicity, I’ve created both Message Producer and Consumer in a single Java file. Please go through those two sub-sections(separated by comments) for understanding purpose.

    Final Project Structure:-

    If we observe the project in Eclipse IDE, it’s final structure looks like as below:

    Execute and Observe the Output:-

    In this section, we will run our JMS Program and observe the results in Eclipse IDE Console.

    That’s it all about developing Simple JMS API 1.1 Producer and Consumer application with Embedded JBoss HornetQ Server. We will discuss JMS API 2.0 Producer and Consumer Example in my coming posts.

    Please drop me a comment if you like my post or have any issues/suggestions.

    Leave a Reply

    Your email address will not be published. Required fields are marked *