A simple JMS client for WebLogic 11g

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

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.

Preparation

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.

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

About Mark Nelson

Mark Nelson is an Architect ("IC6") in the Platform Architecture Team in Oracle Development. Mark's focus area is continuous delivery, configuration management and provisioning - making it simple to manage the configuration of complex environments and applications built with Oracle Database, Fusion Middleware and Fusion Applications, on-premise and in the cloud. Before joining the Platform Architecture team, Mark was a senior member of the A-Team since 2010, and worked in Sales Consulting at Oracle since 2006 and various roles at IBM since 1994.
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

10 Responses to A simple JMS client for WebLogic 11g

  1. Pingback: Using JMS in .Net Applications « RedStack

  2. Pingback: An updated simple WebLogic JMS client in .Net (C#) « RedStack

  3. Pingback: Simple JMS client in Scala | RedStack

  4. Pingback: An updated simple WebLogic JMS client | RedStack

  5. Pingback: Sending JMS messages from Scala – Learn Scala

  6. Hi,

    Above post is very useful. It just worked like a charm. Thank you so much!
    keep posting more :)

    Regards,
    Jagadish

  7. Pingback: Simple JMS with Weblogic 11g « Java and Stuff

  8. Pingback: Desktop Events with OpenSpan and Oracle CEP – Part 1

  9. Suhash BHattacharjee says:

    Hi,
    Thanks a lot for the post.It’s simple , clear and to the point and worked fine.
    Regards
    Suhash Bhattacharjee

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s