Date Created: Mon 05-Sep-2011

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


    WebLogic JMS Java client example using JDeveloper

    using this article http://redstack.wordpress.com/2009/12/21/a-simple-jms-client-for-weblogic-11g/ as a reference I am going to demonstrate how to build and create a sample JMS application to be deployed on WebLogic 11g. (10.3.3)

    File-New



    General>Projects>Java Project



    On the Create Java Project Step 1 of 2 page type a project name for example JMS Client



    On the Create Java Project Step 2 of 2, set the package name to be com.screv.jms



    Add a class to the project




    From the General category choose Java and then select Java Class.




    Type JMSTest as the class name



    Paste the following code into the project

    package com.screv.jms;

    import java.util.Hashtable;

    import javax.naming.*;

    import javax.jms.*;

    public class JMSTest {
    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/myQueueConnectionFactory";
    private static final String QUEUE_NAME = "jms/myTestQueue";

    public JMSTest() {
    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:7101");
    properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
    properties.put(Context.SECURITY_CREDENTIALS, "weblogic123");
    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");
    }
    }


    We now need to ensure that we have the appropriate JAR file to access the JMS API. Remember it is the application servers' responsibility to provide the JMS provider so our client needs to use a WL JAR file. We can use the WebLogic 10.3 Thin Client JAR as provided by JDeveloper. alternatively we could reference a local WL installation and reference the equivalent JAR file there.




    We now need to create the Queue Connection Factory and Queue in WebLogic

    Log into the admin console to follow these hight level steps/

    1. Create a JMS server and terget it to a managed server called Server01
    2. Create a SubDeployment called mySubDeployment to allow the QCF and Queue to be mapped to the the approrpate JMS server
    3. Create a JMS Module
    4. Add a QCF and Q to the JMS Module


    Creating a JMS Server



    In the summary page of JMS servers, click New



    Type myJMSServer in the name field and click Next

    Note: I have chosen to not use any persistence, this means when the Server01 is restarted the messages on the Queue will be lost. This is OK as we are only trying to proove that our client can indeed connect and send messages to the Queue.



    Ensure that you target an appropriate managed server for example Server01



    Click Finish and the result will be a new JMS Server entry in the list of JMS Servers as seen below.




    Select JMS Modules from the main Left-Hand-Side navigation panel



    Click New to start the creation of a new JMS Module




    Type myJMSModule as the name for the JMS Module as seen below



    Click Next

    Select an appropriate target for the JMS Module (In my example I am using Server01 which is part of a cluster)



    Click Next. Optionally you can choose to move straight onto creating resources ie the Queue Connection Factory and Queue etc


    I chose not to, and the result is as you can see the new JMS Module called myJMSModule.




    Lets now add the resources

    Click On myJMSModule

    Then select the SubDeployments tab



    Click New to add a new Sub Deployment




    Type mySubDeployment as the name and click Next. On the next screen we will set the myJMSServer as the target.



    Click Finish, you can see the new sub deployment below




    Navigate back to myJMSModule and click New to add a Queue Connection Factory



    Select Connection Factory


    Type myQueueConnectionFactory as the name and click Finish



    Click Finish, now go back into the QCF definition and change the default target, then its target to the sub deployment

    On the Configuration tab for the QCF uncheck the Default Target Enabled checkbox. Also remember to fill in the JNDI name as jms/myQueueConnectionFactory






    Click Save

    The result should be now that you have a new QCF called MyQueueConnectionFactory and





    OK, now lets follow the same process and create a new Queue.





    Type myTestQueue as the Queue name and jms/myTestQueue as the JNDI name



    Click Next and assign to the same sub deployment as the QCF




    Result





    We are now ready. If you go back to Jdevloper and run the application we created earlier, then you will get the following error





    d:\oracle\middleware_1033\jdk160_18\bin\javaw.exe -client -classpath C:\JDeveloper\mywork\JSTExample\.adf;C:\JDeveloper\mywork\JSTExample\JMSClient\classes;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wlclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wljmxclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wljmsclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wlnmclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wlsafclient.jar -Djavax.net.ssl.trustStore=D:\oracle\middleware_1033\wlserver_10.3\server\lib\DemoTrust.jks com.screv.jms.JMSTest
    Got InitialContext javax.naming.InitialContext@cd2e33
    javax.naming.NameNotFoundException: Exception in lookup.: `jms/myQueueConnectionFactory' could not be found. [Root exception is weblogic.corba.cos.naming.NamingContextAnyPackage.NotFound: IDL:weblogic/corba/cos/naming/NamingContextAny/NotFound:1.0]
    at weblogic.corba.j2ee.naming.Utils.wrapNamingException(Utils.java:65)
    at weblogic.corba.j2ee.naming.ContextImpl.lookup(ContextImpl.java:289)
    at weblogic.corba.j2ee.naming.ContextImpl.lookup(ContextImpl.java:227)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.screv.jms.JMSTest.sendMessage(JMSTest.java:45)
    at com.screv.jms.JMSTest.main(JMSTest.java:126)
    Caused by: weblogic.corba.cos.naming.NamingContextAnyPackage.NotFound: IDL:weblogic/corba/cos/naming/NamingContextAny/NotFound:1.0
    at weblogic.corba.cos.naming.NamingContextAnyPackage.NotFoundHelper.read(NotFoundHelper.java:72)
    at weblogic.corba.cos.naming._NamingContextAnyStub.resolve_any(_NamingContextAnyStub.java:87)
    at weblogic.corba.j2ee.naming.ContextImpl.lookup(ContextImpl.java:267)
    ... 4 more
    Process exited with exit code 0.


    This is because we now need to restart the Server01.

    After the server is restarted the result of a single application run through tis as follows:

    d:\oracle\middleware_1033\jdk160_18\bin\javaw.exe -client -classpath C:\JDeveloper\mywork\JSTExample\.adf;C:\JDeveloper\mywork\JSTExample\JMSClient\classes;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wlclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wljmxclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wljmsclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wlnmclient.jar;D:\oracle\middleware_1033\wlserver_10.3\server\lib\wlsafclient.jar -Djavax.net.ssl.trustStore=D:\oracle\middleware_1033\wlserver_10.3\server\lib\DemoTrust.jks com.screv.jms.JMSTest
    Got InitialContext javax.naming.InitialContext@cd2e33
    Got QueueConnectionFactory weblogic.jms.client.JMSXAConnectionFactory@ba5bdb
    Got QueueConnection weblogic.jms.client.WLConnectionImpl@c3e9e9
    Got QueueSession weblogic.jms.client.WLSessionImpl@1d0d124
    Got Queue myJMSModule!myTestQueue
    Got QueueSender weblogic.jms.client.WLProducerImpl@147358f
    Got TextMessage TextMessage[null, null]
    Set text in TextMessage TextMessage[null, test]
    Sent message
    Cleaned up and done.
    Process exited with exit code 0.


    Browsing for messages and the JNDI tree.

    Lets look at the Server01's JNDI tree.



    Locate the Server, then select the server and then go to the server's setting page and click the view JNDI link.



    We get a new window which allows us to browse this server's JNDI Tree, which can be very useful to esnure you have configured resources correctly.





    Lets now look at the JMS Server's queue depth

    Home >myJMSModule >myQueueConnectionFactory >myJMSModule >Summary of Servers >Server01 >Summary of JMS Servers >myJMSServer >JMS Modules >myJMSModule >myTestQueue



    Anyway, we are all now done, so hope this helps.

    Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Oracle WebLogic Categories

JBoss Categories

Other Categories