JMS Step 2 - Using the Sample Program to Send a Message to a JMS Queue

JMS Step 2 - Using the Sample Program to Send a Message to a JMS Queue

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

In the previous post
JMS Step 1 - How to Create a Simple JMS Queue in Weblogic Server 11g
I showed you how to create a JMS queue and its dependent objects in WebLogic Server. In this article, we will use a sample program to write a message to that queue. Please review the previous post if you have not created those objects yet, as they will be required later in this example. The previous post also includes useful background information and links to the Oracle documentation for addional research.

The following post in this series will show how to read the message from the queue again.

1. Source code

The following java code will be used to write a message to the JMS queue. It is based on a sample program provided 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
* and send messages to the 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. The class is used to send messages to the queue.
* @author Copyright (c) 1999-2005 by BEA Systems, Inc. All Rights Reserved.
public class QueueSend
 // Defines the JNDI context factory.
 public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

 // Defines the JMS context factory.
 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 QueueSender qsender;
 private Queue queue;
 private TextMessage msg;

  * Creates all the necessary objects for sending
  * messages to 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);
    qsender = qsession.createSender(queue);
    msg = qsession.createTextMessage();

  * Sends a message to a JMS queue.
  * @param message  message to be sent
  * @exception JMSException if JMS fails to send message due to internal error
 public void send(String message) throws JMSException {

  * 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 operation fails
 public static void main(String[] args) throws Exception {
    if (args.length != 1) {
     System.out.println("Usage: java examples.jms.queue.QueueSend WebLogicURL");
    InitialContext ic = getInitialContext(args[0]);
    QueueSend qs = new QueueSend();
    qs.init(ic, QUEUE);

 private static void readAndSend(QueueSend qs)
    throws IOException, JMSException
    BufferedReader msgStream = new BufferedReader(new InputStreamReader(;
    String line=null;
    boolean quitNow = false;
    do {
     System.out.print("Enter message (\"quit\" to quit): \n");
     line = msgStream.readLine();
     if (line != null && line.trim().length() != 0) {
       System.out.println("JMS Message Sent: "+line+"\n");
       quitNow = line.equalsIgnoreCase("quit");
    } while (! quitNow);


 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 UNIX/Linux

Log in to a machine with a WebLogic installation and create a directory to contain the source and code matching the package name, e.g. $HOME/examples/jms/queue. Copy the above 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

    . ./
  2. Collect the following information required to run the script:
  1. The JNDI name of a 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,
  2. The JNDI name of a connection factory 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
  3. 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 given to the QueueSend program in this example will therefore be t3://host.domain:8001
  1. Edit 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 using

  2. Go to the source’s top-level directory and execute it using

    java examples.jms.queue.QueueSend t3://
  3. This will prompt for a text input or “quit” to end.
  4. In the WLS console, go to the queue and select Monitoring to confirm that a new message was written to the queue.

2.2 From JDeveloper

  1. Create a new application in JDeveloper, called, for example JMSTests.
  2. When prompted for a project name, enter QueueSend and select Java as the technology

  3. Default Package = examples.jms.queue (but you can enter anything here as you will overwrite it in the code later).
    Leave the other values at their defaults.

  4. Press Finish
  5. Create a new Java class called QueueSend and use the default values

    This will create a file called
  6. Open, if it is not already open and replace all its contents with the QueueSend java code listed above

  7. Some lines might have warnings due to unfound objects.
    These are due to missing libraries in the JDeveloper project.

  8. Add the following libraries to the JDeveloper project:
  1. right-click the QueueSend  project in the navigation menu and select Libraries and Classpath , then Add JAR/Directory
  2.  Go to the folder containing the JDeveloper installation and find/choose the file javax.jms_1.1.1.jar , e.g. at D:\oracle\jdev11116\modules\javax.jms_1.1.1.jar

    Do the same for the weblogic.jar file located, for example in D:\oracle\jdev11116\wlserver_10.3\server\lib\weblogic.jar

  1. Now you should be able to compile the project, for example by selecting the Make or Rebuild icons

  2. If you try to execute the project, you will get a usage message, as it requires a parameter pointing to the WLS installation containing the JMS queue, for example t3:// . You can automatically pass this parameter to the program from JDeveloper by editing the project’s Run/Debug/Profile.
    Select the project properties, select Run/Debug/Profile and edit the Default run configuration

    and add the connection parameter to the Program Arguments field

    If you execute it again, you will see that it has passed the parameter to the start command

  3. If you get a ClassNotFoundException for the class weblogic.jndi.WLInitialContextFactory , then check that the weblogic.jar file was correctly added to the project in one of the earlier steps above.

Set the values of JMS_FACTORY and QUEUE the same way as described above in the description of how to use this from a Linux file system, i.e.

public final static String  JMS_FACTORY="
jms/TestConnectionFactory ";

public final static String QUEUE="
jms/TestJMSQueue ";


  1. You need to make one more change to the project. If you execute it now, it will prompt for the payload for the JMS message, but you won’t be able to enter it by default in JDeveloper. You need to enable program input for the project first.

    Select the project’s properties, then Tool Settings, then check the Allow Program Input checkbox at the bottom and Save.

  2. Now when you execute the project, you will get a text entry field at the bottom into which you can enter the payload. You can enter multiple messages until you enter “quit”, which will cause the program to stop.

The following screen shot shows the TestJMSQueue’s Monitoring page, after a message was sent to the queue:

This concludes the sample. In the following post I will show you how to read the message from the queue again.


Post a Comment:
  • HTML Syntax: NOT allowed

This is the official blog of the SOA Proactive Support Team. Here we will provide information on our activities, publications, product related information and more. Additionally we look forward to your feedback to improve what we do.


« April 2014