jms

Java JMS “HelloWorld” on JBoss Example

In this article, I am going to show a simple “Hello World” example using JBoss Messaging. Before we get started with it, let me first brief you about JMS. The Java Message Service, JMS in short, is a standard Java API that allows components to communicate with each other using messaging. You have four main components:

JMS Provider is the component that mediates the messages between producer and consumer. It implements the JMS specification and acts as a server. Below are the main components involved:

  • Producer sends message to a specific JMS destination
  • Consumer listens on a JMS destination to receive the message
  • Destination where the message is sent, can be queue or a topic

For this tutorial, we will be use the following tools:

  • Eclipse Luna 4.4.1
  • JBoss 5.0 AS
  • JDK 1.7
Tip
You may skip project setup and jump directly to the beginning of the example below.

1. Create a new Java Project

Since we will be creating a producer and consumer, we also need to include ‘JBoss 5.0 Runtime’ to our libraries.

Open ‘Java Build Path’, Click on ‘Add Library’->’Server Runtime’->next->’JBoss 5.0 Runtime’.

First open ‘Java Build Path’.

jms_java_build_path
Figure 1: Java Build Path

 

Click on ‘Add Library’, select ‘Server Runtime’ and click on ‘Next’

jms_select_server_runtime
Figure 2: Select Server Runtime 

Select ‘JBoss 5.0 Runtime’ and click on finish.

jms_add_JBoss_5_0_lib
Figure 3: Add JBoss 5.0 Runtime

 

After adding ‘JBoss 5.0 Runtime’, the build path should look like below:

jms_build_path_after_adding_jboss_5_0
Figure 4: Build Path after adding JBoss 5.0 Runtime

 

2. Add a new destination queue

Open xml file ${JBOSS_HOME}\server\default\deploy\messaging\destinations-service.xml and copy paste the below mbean element.

   <mbean code="org.jboss.jms.server.destination.QueueService"
      name="jboss.messaging.destination:service=Queue,name=HelloWorldQueue"
      xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
      <depends>jboss.messaging:service=PostOffice</depends>
   </mbean>

Notice the name attribute name="jboss.messaging.destination:service=Queue,name=HelloWorldQueue". Since queue will be the Top level domain and HelloWorldQueue will be the middle level domain, our queue name will be combination of both /queue/HelloWorldQueue

3. Start JBoss

Once we have added our own ‘HelloWorldQueue’ queue, we will start the JBoss. You can either start it from eclipse or simply double click on ${JBOSS_HOME}/bin/run.bat.

4. Producer

We will now write our producer.

HellowWorldProducer:

package com.javacodegeeks.examples;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.NamingException;

public class HelloWorldProducer {
	public static void main(String[] args) throws NamingException, JMSException {
		Connection connection = null;
		try {
			System.out.println("Create JNDI Context");
			Context context = ContextUtil.getInitialContext();
			
			System.out.println("Get connection facory");
			ConnectionFactory connectionFactory = (ConnectionFactory) context
					.lookup("ConnectionFactory");
			
			System.out.println("Create connection");
			connection = connectionFactory.createConnection();
			
			System.out.println("Create session");
			Session session = connection.createSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);
			
			System.out.println("Lookup queue");
			Queue queue = (Queue) context.lookup("/queue/HelloWorldQueue");
			
			System.out.println("Start connection");
			connection.start();
			
			System.out.println("Create producer");
			MessageProducer producer = session.createProducer(queue);
			
			System.out.println("Create hello world message");
			Message hellowWorldText = session.createTextMessage("Hello World!");
			
			System.out.println("Send hello world message");
			producer.send(hellowWorldText);	
		} finally {
			if (connection != null) {
				System.out.println("close the connection");
				connection.close();
			}

		}

	}
}

5. JNDI Context

Since both producer and consumer needs JNDI context, the Context creation is in a common utility class. Here is the code that returns us InitialContext. Notice that java.naming.provider.url is set to localhost:1099 as JBossMessaging is running in local machine at port 1099.

ContextUtil:

package com.javacodegeeks.examples;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ContextUtil {
	public static Context getInitialContext() throws NamingException {
		Properties props = new Properties();
		props.setProperty("java.naming.factory.initial",
				"org.jnp.interfaces.NamingContextFactory");
		props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
		props.setProperty("java.naming.provider.url", "localhost:1099");
		Context context = new InitialContext(props);
		return context;
	}
}

Before we start coding our consumer, few important points to note from the HelloWorldProducer class.

  1. We first need a JNDI context
    Context context = ContextUtil.getInitialContext();
  2. Using the context we look up for the ConnectionFatory as our goal as a client (producer or consumer) is to first connect to the JMS server.
    ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory")
  3. We will ask the connectionFactory to create a Connection object.
  4. We will use the Connection object to create a Session
  5. In order for the producer to send the message, it needs to know the destination (queue) to which the message should be sent.
  6. Using our queue name, we will do a lookup on the context to find our Queue
  7. connection.start() starts the connection. You can imagine this as socket connection from client to server.

Till this point, even consumer will do exactly same as the producer.

  1. session.createProducer(queue) creates the producer. Note that the destination is passed as an argument
  2. Next, we will create a text message session.createTextMessage("Hello World!")
  3. Finally, we will call producer.send(hellowWorldText); to send the message.

6. Consumer

Below is the Consumer

HelloWorldConsumer:

package com.javacodegeeks.examples;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.NamingException;

public class HelloWorldConsumer implements MessageListener {
	public static void main(String[] args) throws NamingException, JMSException {
		Connection connection = null;
		try {
			System.out.println("Create JNDI Context");
			Context context = ContextUtil.getInitialContext();
			
			System.out.println("Get connection facory");
			ConnectionFactory connectionFactory = (ConnectionFactory) context
					.lookup("ConnectionFactory");
			
			System.out.println("Create connection");
			connection = connectionFactory.createConnection();
			
			System.out.println("Create session");
			Session session = connection.createSession(false,
					QueueSession.AUTO_ACKNOWLEDGE);
			
			System.out.println("Lookup queue");
			Queue queue = (Queue) context.lookup("/queue/HelloWorldQueue");	
			
			System.out.println("Start connection");
			connection.start();
			
			System.out.println("Create consumer");
			MessageConsumer consumer = session.createConsumer(queue);
			
			System.out.println("set message listener");
			consumer.setMessageListener(new HelloWorldConsumer());			
		} finally {
			if (connection != null) {
				System.out.println("close the connection");
				connection.close();
			}
		}
	}

	@Override
	public void onMessage(Message message) {
		try {
			System.out.println("message received");
			System.out.println(((TextMessage) message).getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}

Few points to note:

    1. We call session.createConsumer(queue) to create a MessageConsumer
    2. Since the message consumer is going to listen for a new message, we will have to set a MessageListener
    3. Any class which implements MessageListener can act as a listener on the queue for new message
    4. To keep our example simple, we will make our HelloWorldConsumer implement MessageListener
    5. The message ‘Hello World!’ is received in onMessage(message) callback wrapped in type TextMessage
    6. The actual text message ‘Hello World!’ is retrieved by calling getText() on TextMessage

7. Run the Example

To test this out, we will first run HelloWorldProducer and then run HelloWorldConsumer

Producer Output

Create JNDI Context
Get connection facory
Create connection
Create session
Lookup queue
Start connection
Create producer
Create hello world message
Send hello world message
close the connection

Consumer Output

Create JNDI Context
Get connection facory
Create connection
Create session
Lookup queue
Start connection
Create consumer
set message listener
close the connection
message received
Hello World!

Download the Eclipse project of this tutorial

This was an example of how to create a JMS ‘Hello World’ example using JBoss Messaging

Download
You can download the full source code of this example here: jmshelloworld.zip

Ram Mokkapaty

Ram holds a master's degree in Machine Design from IT B.H.U. His expertise lies in test driven development and re-factoring. He is passionate about open source technologies and actively blogs on various java and open-source technologies like spring. He works as a principal Engineer in the logistics domain.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
priyanka
priyanka
4 years ago

i am getting Exception in thread “main” javax.naming.NameNotFoundException: HelloWorldQueue not bound error while running producer part.
please let me know the solution.

Back to top button