X

Best Practices from Oracle Development's A‑Team

An updated simple WebLogic JMS client

Mark Nelson
Architect

Introduction

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.

Main Article

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.

# 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 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");  } }

Join the discussion

Comments ( 1 )
  • Devesh Wednesday, May 8, 2019
    Hi,

    Can you please update the article with a working link to the code used for the Oracle UCM case study you mentioned for writing to JMS queue. Or can you give me the link to the new post where this is present.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha