Best Practices from Oracle Development's A‑Team

A simple JMS client for WebLogic 11g

Mark Nelson

UPDATE: An update to this simple client is posted here.


This post serves a tutorial on JMS client development and WebLogic JMS resource definitions.

Main Article

Quite often I find that I want to be able to send some JMS messages programmatically, so I decided to write a simple, reusable Java class to help with this need.  In this example, I am using WebLogic Server 11g as my JMS server.


First, I will set up a JMS Queue on my WebLogic Server.  This is done through the WebLogic Server console, which is normally accessible at http://your.server.name:7001/console, using an adminstrative user like weblogic.

After you log in, navigate to Services -> Messaging -> JMS Modules in the menu on the left hand side of the console.


You will see one or more modules listed.  Your list will almost certainly be different to mine.  If you are not sure which one to use, take a look at the settings.  The most important thing to note is the port that is used to connect to the server that your JMS module is deployed on.  You will need that later.  You can find that by clicking on the name of the JMS Module, then going to the Targets tab.  Then go to Environment -> Servers in the main menu and read the port number of the server you saw selected as the Target for the JMS Module.  In my case, it is 9001.


Click on the name of the JMS Module  to display the resources.  You will most likely see a mixture of Queues and Topics and one or more Connection Factories.  Note that you will need to have a QueueConnectionFactory for this example to work.  You should have one there already, take a note of its JNDI name.  In my example it is jms/QueueConnectionFactory.


Now we will create the Queue.  Click on the New button to start.


Select Queue as the type of resource we want to create.  Click on Next.


Enter the Name and JNDI Name for the new queue.  I have followed the convention of using the same name for each, but with “jms/” prepended to the JNDI Name.  Click on Next.


Select your Subdeployment from the drop down list and the JMS Server in the Targets section.  Note that you need to choose the same one as before.  Then click on Finish to create the Queue.  You should now see your new Queue listed in the resources page, as shown below.


JMSSender Class

Here is the Java Class that provides the simple JMS client.  There are two libraries (jar files) that are needed to compile and run this class.  These are located in your WebLogic Server directory:

  • <WebLogic_Directory>\server\lib\wlclient.jar
  • <WebLogic_Directory>\server\lib\wljmsclient.jar

Update: In newer versions of WebLogic Server, there is a new lightweight library called wlthint3client.jar (in the same location) that you can use instead of these libraries.

In JDeveloper, right click on the project and select Project Properties… from the popup menu.  Navigate to the Libraries and Classpath page.  Click on the Add Jar/Directory… button and navigate to the libraries mentioned above to add them to your project.

Then create a new Java Class and add the code below.  Take note of the two comments near the beginning of the file with “NOTE:” in them.  You will need to change five lines of code here to make this code work.

# Copyright 2012 Oracle Corporation. # All Rights Reserved. # # Provided on an 'as is' basis, without warranties or conditions of any kind, # either express or implied, including, without limitation, any warranties or # conditions of title, non-infringement, merchantability, or fitness for a # particular purpose. You are solely responsible for determining the # appropriateness of using and assume any risks. You may not redistribute. package bamrunner; import java.util.Hashtable; import javax.naming.*; import javax.jms.*; public class JMSSender {    private static InitialContext ctx = null;    private static QueueConnectionFactory qcf = null;    private static QueueConnection qc = null;    private static QueueSession qsess = null;    private static Queue q = null;    private static QueueSender qsndr = null;    private static TextMessage message = null;    // NOTE: The next two lines set the name of the Queue Connection Factory    //       and the Queue that we want to use.    private static final String QCF_NAME = "jms/QueueConnectionFactory";    private static final String QUEUE_NAME = "jms/OPOQueue";    public JMSSender() {        super();    }    public static void sendMessage(String messageText) {        // create InitialContext        Hashtable properties = new Hashtable();        properties.put(Context.INITIAL_CONTEXT_FACTORY,                       "weblogic.jndi.WLInitialContextFactory");        // NOTE: The port number of the server is provided in the next line,        //       followed by the userid and password on the next two lines.        properties.put(Context.PROVIDER_URL, "t3://localhost:9001");        properties.put(Context.SECURITY_PRINCIPAL, "weblogic");        properties.put(Context.SECURITY_CREDENTIALS, "welcome1");        try {            ctx = new InitialContext(properties);        } catch (NamingException ne) {            ne.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got InitialContext " + ctx.toString());        // create QueueConnectionFactory        try {            qcf = (QueueConnectionFactory)ctx.lookup(QCF_NAME);        }        catch (NamingException ne) {            ne.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got QueueConnectionFactory " + qcf.toString());        // create QueueConnection        try {            qc = qcf.createQueueConnection();        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got QueueConnection " + qc.toString());        // create QueueSession        try {            qsess = qc.createQueueSession(false, 0);        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got QueueSession " + qsess.toString());        // lookup Queue        try {            q = (Queue) ctx.lookup(QUEUE_NAME);        }        catch (NamingException ne) {            ne.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got Queue " + q.toString());        // create QueueSender        try {            qsndr = qsess.createSender(q);        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got QueueSender " + qsndr.toString());        // create TextMessage        try {            message = qsess.createTextMessage();        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Got TextMessage " + message.toString());        // set message text in TextMessage        try {            message.setText(messageText);        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Set text in TextMessage " + message.toString());        // send message        try {            qsndr.send(message);        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);            System.exit(0);        }        System.out.println("Sent message ");        // clean up        try {            message = null;            qsndr.close();            qsndr = null;            q = null;            qsess.close();            qsess = null;            qc.close();            qc = null;            qcf = null;            ctx = null;        }        catch (JMSException jmse) {            jmse.printStackTrace(System.err);        }        System.out.println("Cleaned up and done.");    }    public static void main(String args[]) {        sendMessage("test");    } }

How to use it

The code shown above has a main() method included, which will send a simple message containing the word “test.”  You can just right click on the Class and run it to execute the main() method.

After you do this, you will be able to see your message in the queue (assuming everything worked).  To do this, click on the name of your queue in the resources page (where we left the console earlier), then select the Monitoring tab.  You will see the details of messages in the queue.  Select the queue and click on the Show Messages button.


You will see a list of the messages.  You can click on the message ID to drill down to more details, including the data.


Here you see the JMS header information and the data in the message body (payload):


Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha