Wednesday, November 2, 2011

JAVA API to verify JMS Consumer

Sometime it happens that JMS Consumer (BPEL/ESB Process) stops consuming message from the JMS Queue. This queue could be either on the SOA Side or on participating application side. To narrow down to the root cause of the issue, below JAVA API will be highly useful. The JAVA API will consume the data from the queue if queue is working properly so in that case you need to look into your consumer process to trace out the root cause of the issue. In case the JAVA API is throwing any error then it means there is an issue at the queue end which needs to be sorted out.

To set up the enviroment to execute this JAVA API please go through the previous post "Test your JMS Queue" and follow step no. 1 (Environment setup).

2. Create a JAVA file with name TestJMSConsumer.java and copy the below code in it

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.*;

public class TestJMSConsumer
{

  public static void main(String[] args)
  {

    //*******************************************************************************************
    //* Change settings below according to JMS provider requirements
    //*******************************************************************************************

    // Set Context Factory
    final String myContextFactoryName = "com.evermind.server.rmi.RMIInitialContextFactory";

    // Set the target queue connection factory
    final String myQueueConnectionFactoryName = "java:comp/resource/COMMS_SUBMITORDER_PRODUCER/XAQueueConnectionFactories/AIA_Queue";

    // Set the target queuename
    final String myQueueName = "java:comp/resource/COMMS_SUBMITORDER_PRODUCER/Queues/JMSUSER.AIA_CRTFO_IN_JMSQ";

    // Set UrlProvider
    final String myUrlProviderName = "opmn:ormi://soaserver:6003:oc4j_soa/default";

    // Set user+password credentials
    final String myUser     = "oc4jadmin";
    final String myPassword = "password";

    //*******************************************************************************************

    String classPath = System.getProperty("java.class.path",".");

    Context                jndiContext = null;
    QueueConnectionFactory myQueueConnectionFactory = null;
    QueueConnection        myQueueConnection = null;
    QueueSession           myQueueSession = null;
    Queue                  myQueue = null;
    QueueReceiver          myQueueReceiver = null;


    /*
     * Set the environment for a connection to the JMS server
     */
    Hashtable myEnv = new Hashtable();
              myEnv.put(Context.INITIAL_CONTEXT_FACTORY, myContextFactoryName);
              myEnv.put(Context.SECURITY_PRINCIPAL,      myUser);
              myEnv.put(Context.SECURITY_CREDENTIALS,    myPassword);
              myEnv.put(Context.PROVIDER_URL,            myUrlProviderName);

    System.out.println("Using :-");
    System.out.println("Context Factory=" + myContextFactoryName);
    System.out.println("Queue Connection Factory=" + myQueueConnectionFactoryName);
    System.out.println("Url Provider=" + myUrlProviderName);
    System.out.println("");
    System.out.println("Current CLASSPATH=" + classPath);
    System.out.println("");

    /*
     * Set the Context Object.
     * Lookup the Queue Connection Factory.
     * Lookup the Queue
     */
    try
    {
       jndiContext = new InitialContext(myEnv);
       System.out.println("Lookup Queue Connection Factory : " + myQueueConnectionFactoryName);

       myQueueConnectionFactory = (QueueConnectionFactory)jndiContext.lookup(myQueueConnectionFactoryName);
       System.out.println("OK");
       System.out.println("Lookup Queue " + myQueueName);

       myQueue = (Queue)jndiContext.lookup(myQueueName);
       System.out.println("OK");
    }
    catch (NamingException e)
    {
       System.out.println("JNDI lookup failed: " + e.toString());
       System.exit(1);
    };

    /*
     * Create connection factory, session, receiver and receive message
     */
    try
    {
       myQueueConnection = myQueueConnectionFactory.createQueueConnection();
       myQueueSession    = myQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
       myQueueReceiver  = myQueueSession.createReceiver(myQueue);

       // Start the queue connection
       myQueueConnection.start();

       System.out.println("Receiving Message...");

       TextMessage message = (TextMessage) myQueueReceiver.receive();
       System.out.println("Received Message: " + message.getText());

       // Close the queue connection
       myQueueConnection.close();

  }
    catch (JMSException e)
    {
       System.out.println("Exception occurred: " + e.toString());
    }
    finally
    {
       if (myQueueConnection != null)
       try
       {
         myQueueConnection.close();
       }
       catch (JMSException e)
       {
        System.out.println("Close error: " + e.toString());
       };
    };
  }; // end main
}// end TestJMSConsumer class


3. Modify the parameters mentioned below:

• ORACLE_HOME

• myQueueConnectionFactoryName

• myQueueName

• myUrlProviderName

• myPassword



4. Steps to execute the code:

[oracle@soaserver JMSClient]$ source setpath.sh

[oracle@soaserver JMSClient]$ javac TestJMSConsumer.java

[oracle@soaserver JMSClient]$ java TestJMSConsumer