Sunday, October 16, 2011

Test your JMS Queue

Test your JMS Queue:


Script location: /home/oracle/scripts/JMS/JMSClient

1. Environment setup:

a. Create a script (setpath.sh) to set the environment:

export ORACLE_HOME=/u01/app/aia/soa

export CLASSPATH=.:$ORACLE_HOME/lib/xml.jar:$ORACLE_HOME/lib/xmlparserv2.jar:$ORACLE_HOME/bpel/system/services/lib/bpm-services.jar:$ORACLE_HOME/bpel/lib/bpm-infra.jar:$ORACLE_HOME/bpel/lib/connector15.jar:$ORACLE_HOME/bpel/lib/oracle_http_client.jar:$ORACLE_HOME/j2ee/home/oc4jclient.jar:$ORACLE_HOME/bpel/lib/orawsdl.jar:$ORACLE_HOME/bpel/lib/orabpel.jar:$ORACLE_HOME/bpel/lib/orabpel-common.jar:$ORACLE_HOME/bpel/lib/orabpel-thirdparty.jar:$ORACLE_HOME/integration/esb/lib/commons-logging.jar:$ORACLE_HOME/integration/esb/lib/commons-codec-1.3.jar:$ORACLE_HOME/j2ee/home/admin_client.jar:$ORACLE_HOME/adminclient.jar:$ORACLE_HOME/argus.jar:$ORACLE_HOME/j2ee/home/lib/jmx_remote_api.jar:$ORACLE_HOME/j2ee/home/lib/jmxcluster.jar:$ORACLE_HOME/j2ee/home/lib/jmxri.jar:$ORACLE_HOME/j2ee/home/oc4jclient.jar:$ORACLE_HOME/opmn/lib/ons.jar:$ORACLE_HOME/opmn/lib/opmnconfig.jar:$ORACLE_HOME/opmn/lib/optic.jar:$ORACLE_HOME/opmn/lib/repositorycheck.jar:$ORACLE_HOME/j2ee/home/lib/jms.jar:$ORACLE_HOME/j2ee/home/lib/bcel.jar:$ORACLE_HOME/j2ee/home/lib/connector.jar

export PATH=$ORACLE_HOME/jdk/bin:$PATH



b. Copy oc4j-internal.jar in the folder JMSClient

[oracle@soaserver JMSClient]$ cp $ORACLE_HOME/j2ee/home/lib/oc4j-internal.jar .

c. Execute below commands

[oracle@soaserver JMSClient]$ source setpath.sh

[oracle@soaserver JMSClient]$ jar vxf oc4j-internal.jar com/evermind/util/JCAProperties.class



2. Create a JAVA file with name TestJMSClient.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 TestJMSClient

{



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/SiebelJmsRP/QueueConnectionFactories/QCF";



// Set the target queuename

final String myQueueName = "java:comp/resource/SiebelJmsRP/Queues/AIA_SALESORDERJMSQUEUE";



// 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;

QueueSender myQueueSender = null;

BytesMessage myBytesMessage = 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, sender and send message

*/

try

{

myQueueConnection = myQueueConnectionFactory.createQueueConnection();

myQueueSession = myQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

myQueueSender = myQueueSession.createSender(myQueue);

myBytesMessage = myQueueSession.createBytesMessage();



System.out.println("Sending message...");



String stringMessage = " ";

byte[] byteData = stringMessage.getBytes();

myBytesMessage.writeBytes(byteData);

myQueueSender.send(myBytesMessage);

System.out.println("OK"+stringMessage);

System.out.println("Sent message: " + " - " + myBytesMessage.getJMSMessageID());

}

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());

};

};

};

}



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 TestJMSClient.java

Note: TestJMSClient.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

[oracle@soaserver JMSClient]$ java TestJMSClient