An updated simple WebLogic JMS client

A little while ago, we posted a simple JMS client for WebLogic here.  That post demonstrated how to create a simple JMS client program that could send messages to a WebLogic JMS queue.  It also showed how to set up the necessary definitions on the WebLogic Server in the first place, and how to see the details of the messages after they had been sent.

In that post we mentioned that there are often times when we want to be able to send messages to a JMS queue.  One such time was recently when we wanted Oracle Universal Content Management (UCM) to send a JMS message every time a new piece of content was checked in to the content management system.  In order to do that, we reused this code, but it needed a small update to allow us to pass in details of the server, queue name, etc.

This post presents an updated simple JMS client, in Java.  Here it is still general purpose.  In a future post, we will show how it is used to satisfy the use case described above.  We will also present this updated simple client in C# in a separate post.  This post will be updated with links when these new posts are published.

Update: You can grab this code from our Subversion repository:
svn checkout https://www.samplecode.oracle.com/svn/jmsclients/trunk

The C# version is here and a Scala version here.

Please refer to the earlier post for details of how to use this code.

package jmsclient;

import java.util.Hashtable;
import javax.naming.*;
import javax.jms.*;

public class SimpleJMSClient {

  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;
  private static final String DEFAULT_QCF_NAME = "jms/MarksConnectionFactory";
  private static final String DEFAULT_QUEUE_NAME = "jms/MarksQueue";
  private static final String DEFAULT_URL = "t3://localhost:7101";
  private static final String DEFAULT_USER = "weblogic";
  private static final String DEFAULT_PASSWORD =  "weblogic1";

  public SimpleJMSClient() {
    super();
  }

  public static void sendMessage(String messageText) {
    sendMessage(DEFAULT_URL,
    DEFAULT_USER,
    DEFAULT_PASSWORD,
    DEFAULT_QCF_NAME,
    DEFAULT_QUEUE_NAME,
    messageText);
  }

  public static void sendMessage(String url, String user, String password,
                                 String cf, String queue, String messageText) {
    // create InitialContext
    Hashtable properties = new Hashtable();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,
                   "weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL, url);
    properties.put(Context.SECURITY_PRINCIPAL, user);
    properties.put(Context.SECURITY_CREDENTIALS, password);

    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(cf);
    } 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);
    } 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("hello from sendMessage() with 1 arg");
    sendMessage("t3://localhost:7101",
                "weblogic",
                "weblogic1",
                "jms/MarksConnectionFactory",
                "jms/MarksQueue",
                "hello from sendMessage() with 6 args");
  }

}

About Mark Nelson

Mark Nelson is an Architect (an "IC6") in the Fusion Middleware Central Development Team at Oracle. Mark's job is to make Fusion Middleware easy to use in the cloud and at home, for developers and operations folks, with special focus on 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 this 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.

6 Responses to An updated simple WebLogic JMS client

  1. Pingback: A simple JMS client for WebLogic 11g « RedStack

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

  3. Pingback: Improving JMS Performance on WebLogic | RedStack

  4. tyskjohan says:

    Hi

    I can’t get this to work, I get an exception when calling the send(message)

    Set text in TextMessage oracle.jms.AQjmsTextMessage@818e58b
    Exception in thread "Main Thread" weblogic.rmi.extensions.RemoteRuntimeException: Unexpected Exception
    at weblogic.jdbc.rmi.internal.ConnectionImpl_weblogic_jdbc_wrapper_JTAConnection_weblogic_jdbc_wrapper_XAConnection_oracle_jdbc_driver_LogicalConnection_1033_WLStub.physicalConnectionWithin(Unknown Source)
    Caused by: java.rmi.MarshalException: error marshalling return; nested exception is:
    java.io.NotSerializableException: oracle.jdbc.driver.T4CConnection

    What could cause this?

    • Mark Nelson says:

      Hi, Looks like you might be importing the wrong class there. AQ is the messaging provider in the Oracle Database. You want to make sure you are using the normal Java JMS classes. I would go back and check you have the right JAR files in your classpath for your application, and if you are using an IDE, make sure it did not import the wrong classes.

  5. Pingback: Moving and managing messages in JMS Queues | Nitin's Oracle Fusion Middleware and SOA World

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