Wednesday Nov 14, 2012



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
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:


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
* for messages. The class is used to send messages to the queue.
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
 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.
 public void close() throws JMSException {
/** main() method.
 * @param args WebLogic Server URL
 * @exception Exception if operation fails
    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.

    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

    TestConnectionFactory and its JNDI name
    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


    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.




« November 2012 »