Proactive insights, news and tips from Oracle Fusion Middleware Support. Learn Oracle from Oracle.

JMS Step 3 - Using the QueueReceive.java Sample Program to Read a Message from a JMS Queue

This post continues the series of JMS articles which demonstrate
how to use JMS queues in a SOA context.

In the first post,
JMS Step 1 - How to Create a Simple JMS Queue in Weblogic Server 11g
we looked at how to create a JMS queue and its dependent objects in WebLogic Server.

In the previous post,
JMS Step 2 - Using the QueueSend.java Sample Program to Send a Message to a JMS Queue
I showed how to write a message to that JMS queue using the QueueSend.java sample program.

In this article, we will use a similar sample, the QueueReceive.java  program to read the message from that queue.
Please review the previous posts if you have not already done so, as they contain prerequisites for executing the sample in this article.

1. Source code

The following java code will be used to read the message(s) from the JMS queue. As with the previous example, it is based on a sample program shipped with the WebLogic Server installation. The sample is not installed by default, but needs to be installed manually using
the WebLogic Server Custom Installation option, together with many, other useful samples. You can either copy-paste the following code into your editor, or install all the samples.

The knowledge base article in My Oracle Support:

How To Install WebLogic Server and JMS Samples in WLS 10.3.x (Doc ID 1499719.1)

describes how to install the samples.


package examples.jms.queue;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
* This example shows how to establish a connection to
* and receive messages from a JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages.  This class is used to receive and remove messages
* from the queue.
* @author Copyright (c) 1999-2005 by BEA Systems, Inc. All Rights Reserved.
public class QueueReceive implements MessageListener
 // Defines the JNDI context factory.
 public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
 // Defines the JMS connection factory for the queue.
 public final static String JMS_FACTORY="jms/TestConnectionFactory";
 // Defines the queue.
 public final static String QUEUE="jms/TestJMSQueue";
 private QueueConnectionFactory qconFactory;
 private QueueConnection qcon;
 private QueueSession qsession;
 private QueueReceiver qreceiver;
 private Queue queue;
 private boolean quit = false;
 * Message listener interface.
 * @param msg  message
 public void onMessage(Message msg)
    try {
     String msgText;
     if (msg instanceof TextMessage) {
       msgText = ((TextMessage)msg).getText();
     } else {
       msgText = msg.toString();
     System.out.println("Message Received: "+ msgText );
     if (msgText.equalsIgnoreCase("quit")) {
       synchronized(this) {
         quit = true;
         this.notifyAll(); // Notify main thread to quit
    } catch (JMSException jmse) {
     System.err.println("An exception occurred: "+jmse.getMessage());
  * Creates all the necessary objects for receiving
  * messages from a JMS queue.
  * @param   ctx    JNDI initial context
  * @param    queueName    name of queue
  * @exception NamingException if operation cannot be performed
  * @exception JMSException if JMS fails to initialize due to internal error
 public void init(Context ctx, String queueName)
    throws NamingException, JMSException
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
    qcon = qconFactory.createQueueConnection();
    qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    qreceiver = qsession.createReceiver(queue);
  * Closes JMS objects.
  * @exception JMSException if JMS fails to close objects due to internal error
 public void close()throws JMSException
 * main() method.
 * @param args  WebLogic Server URL
 * @exception  Exception if execution fails
 public static void main(String[] args) throws Exception {
    if (args.length != 1) {
     System.out.println("Usage: java examples.jms.queue.QueueReceive WebLogicURL");
    InitialContext ic = getInitialContext(args[0]);
    QueueReceive qr = new QueueReceive();
    qr.init(ic, QUEUE);
        "JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
    // Wait until a "quit" message has been received.
    synchronized(qr) {
     while (! qr.quit) {
       try {
       } catch (InterruptedException ie) {}
 private static InitialContext getInitialContext(String url)
    throws NamingException
    Hashtable env = new Hashtable();
    env.put(Context.PROVIDER_URL, url);
    return new InitialContext(env);

2. How to Use This Class

2.1 From the file system on Linux

This section describes how to use the class from the file system of a WebLogic Server installation.

Log in to a machine with a WebLogic Server installation and create a directory to contain the source and code matching the package name,
$HOME/examples/jms/queue. Copy the above QueueReceive.java file to this directory.

  1. Set the CLASSPATH and environment to match the WebLogic server environment.
    Go to
    $MIDDLEWARE_HOME/user_projects/domains/base_domain/bin and execute
    . ./setDomainEnv.sh
  2. Collect the following information required to run the script:
  • The JNDI name of the JMS queue to use
    In the WebLogic server console > Services > Messaging > JMS Modules > Module name, (e.g. TestJMSModule) > JMS queue name, (e.g. TestJMSQueue)
    select the queue and note its JNDI name,
  • The JNDI name of the connection factory to use to connect to the queue
    Follow the same path as above to get the connection factory for the above queue, e.g. TestConnectionFactory and its JNDI name
  • The URL and port of the WebLogic server running the above queue
    Check the JMS server for the above queue and the managed server it is targeted to, for example soa_server1. Now find the port this managed server is listening on, by looking at its entry under Environment > Servers in the WLS console,
    e.g. 8001
    The URL for the server to be passed to the QueueReceive program will therefore be t3://host.domain:8001
  1. Edit QueueReceive.java and enter the above queue name and connection factory respectively under
public final static String  JMS_FACTORY="jms/TestConnectionFactory";
public final static String QUEUE="jms/TestJMSQueue";
  1. Compile QueueReceive.java using
    javac QueueReceive.java
  2. Go to the source’s top-level directory and execute it using
    java examples.jms.queue.QueueReceive t3://jbevans-lx.de.oracle.com:8001
  3. This will print a message that it is ready to receive messages or to send a “quit” message to end.
  4. The program will read all messages in the queue and print them to the standard output until it receives a message with the payload “quit”.

2.2 From JDeveloper

The steps from JDeveloper are the same as those used for the previous program QueueSend.java, which is used to send a message to the queue. So we won't repeat them here. Please see the previous blog post at
JMS Step 2 - Using the QueueSend.java Sample Program to Send a Message to a JMS Queue
and apply the same steps in that example to the QueueReceive.java program.

This concludes the example. In the following post we will create a BPEL process which writes a message based on an XML schema to the queue.

Join the discussion

Comments ( 2 )
  • guest Tuesday, November 11, 2014


    Thanks alot for the great blog post..Help me alot!!

  • guest Friday, April 24, 2015

    Thanks John,

    Very neatly explained.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.