|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Cannot create JNDI InitialContext! |
« View previous topic :: View next topic » |
Author |
Message
|
ranjesh |
Posted: Wed Jan 02, 2008 8:02 am Post subject: Cannot create JNDI InitialContext! |
|
|
Newbie
Joined: 31 Dec 2007 Posts: 4
|
When tried to connect to websphere MQ through JNDI object i get following error. Kindly help. The code is givern below.
i get message when i run the coe with parameter as -url iiop:iiop://9.126.72.81:2873
caught JMSException: javax.jms.JMSException: Cannot create JNDI InitialContext!
linked exception: javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory [Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory]
finished
import javax.jms.*;
//Imports for JNDI
import javax.naming.*;
import javax.naming.directory.*;
//Used for JNDI initialisation
import java.util.Hashtable;
//The following import would not normally be required in a JMS application,
//but is included here to allow us to bypass the JNDI lookup stage
//import com.ibm.mq.jms.*;
import com.ibm.mq.jms.MQQueueConnectionFactory;
//Import required for program tracing
import com.ibm.mq.jms.services.ConfigEnvironment;
public class PTPSample01
{
// These are used when the -nojndi flag is given.
public static String QMGR = "QM_ORANGE";
public static final String QUEUE = "Q2" ;
//SYSTEM.DEFAULT.LOCAL.QUEUE
// The following can be modified if you want to experiment with
// using other administered objects with different content.
public static final String qcfLookup = "DISPATCHER.CF";
public static final String qLookup = "DISPATCH.INPUT";
public static void main( String[] args )
{
String outString =
"A simple text message from PTPSample01";
Queue ioQueue = null;
QueueSession session = null;
QueueConnection connection = null;
QueueConnectionFactory factory = null;
Context ctx = null;
boolean useJNDI = true;
boolean enableTrace = false;
//String icf = "com.sun.jndi.ldap.LdapCtxFactory";
String icf = "com.ibm.websphere.naming.WsnInitialContextFactory";
String url = null;
// Process each of the command-line arguments in turn
for( int i=0; i<args.length; i++ ) {
String arg = args[i].toLowerCase();
if( arg.equals("-nojndi") ) {
// If the nojndi flag has been set then the program will not
// use JNDI to obtain the administered objects, but will
// create them at runtime
useJNDI = false;
} else if( arg.equals("-m") ) {
// Obtain the name of the queue manager to connect to if we
// are not using JNDI
if (i+1< args.length)
QMGR = args[++i];
else
System.out.println("Ignoring -m flag; no value supplied");
} else if( arg.equals("-t") ) {
// Enable program tracing
enableTrace = true;
} else if( arg.equals("-url") ) {
// Obtain the URL for JNDI operations
if( i+1<args.length )
url = args[++i];
else
System.out.println( "Ignoring -url flag; " +
"no value supplied");
} else if( arg.equals("-icf") ) {
// Obtain the name of the class used for creating JNDI's
// initialContext
if( i+1<args.length )
icf = args[++i];
else
System.out.println( "Ignoring -icf flag; " +
"no value supplied");
} else
// Report and discard any unknown command-line options
System.out.println("Ignoring unknown flag: " + arg );
}
// Enable trace, if this is required
if( enableTrace )
ConfigEnvironment.start();
// For simplicity this sample uses a single try block to catch all
// Exceptions. In a real application you would probably use several
// try blocks, allowing more specific error reporting and more options
// for recovery.
try {
// Get the connection factory
if( useJNDI ) {
// obtain the initial context
ctx = initJNDI(icf, url);
// get the connection factory from JNDI
factory = getConnectionFactoryFromJNDI(ctx, qcfLookup);
} else {
// create a connection factory directly
// Note that this requires a reference to the underlying
// class that implements QueueConnectionFactory
System.out.println("Creating a QueueConnectionFactory");
factory = new MQQueueConnectionFactory();
((MQQueueConnectionFactory)factory).setQueueManager(QMGR);
}
// Create a QueueConnection from the QueueConnectionFactory
System.out.println("Creating a Connection");
connection = factory.createQueueConnection();
// IMPORTANT: Receive calls will be blocked if the connection is
// not explicitly started, so make sure that we do so!
System.out.println("Starting the Connection");
connection.start();
// We now create a QueueSession from the connection. Here we
// specify that it shouldn't be transacted, and that it should
// automatically acknowledge received messages
System.out.println("Creating a Session");
boolean transacted = false;
session = connection.createQueueSession( transacted,
Session.AUTO_ACKNOWLEDGE);
// Get the Queue (the specification of where to send our message)
if( useJNDI ) {
// get the Queue from JNDI
ioQueue = getQueueFromJNDI(ctx, qLookup);
} else {
// Use the session to create the queue object, supplying
// the required MQ-specific parameter
ioQueue = session.createQueue( QUEUE );
}
// We now use the session to create a QueueSender, passing in the
// destination (the Queue object) as a parameter
System.out.println("Creating a QueueSender");
QueueSender queueSender = session.createSender(ioQueue);
// Create a QueueReceiver in the same way
//System.out.println( "Creating a QueueReceiver");
//QueueReceiver queueReceiver = session.createReceiver(ioQueue);
// The session is used to create messages, so create an empty
// TextMessage and fill it with some data
System.out.println( "Creating a TextMessage" );
TextMessage outMessage = session.createTextMessage();
System.out.println("Adding Text");
outMessage.setText(outString);
// Ask the QueueSender to send the message we have created
System.out.println( "Sending the message to " + ioQueue.getQueueName() );
queueSender.send(outMessage);
// Now use the QueueReceiver to retrieve the message, blocking
// for a maximum of 1000ms. The receive call returns when the
// message arrives, or after 1000ms, whichever is sooner
//System.out.println( "Reading the message back again" );
//Message inMessage = queueReceiver.receive(1000);
// Check to see if the receive call has actually returned a
// message. If it hasn't, report this and throw an exception...
/*if( inMessage == null ) {
System.out.println( "The attempt to read the message back again " +
"failed, apparently because it wasn't there");
throw new JMSException("Failed to get message back again");
}*/
// ...otherwise display the message
//System.out.println( "\n" + "Got message"+": "+inMessage);
// Check that the message received (a) is of the correct type,
// and (b) contains the same text as the one sent, reporting the
// result of these two checks
// if( inMessage instanceof TextMessage ) {
// Extract the message content with getText()
// String replyString = ((TextMessage) inMessage).getText();
// Test its equality with the message text sent
// if( replyString.equals(outString) ) {
// System.out.println("Reply string equals original string");
// } else {
// If they differ, print them both out
// System.out.println("Error! Reply string differs from " +
// "original string");
// System.out.println("Original string = '" +
// outString + "'");
// System.out.println("Reply string = '" +
// replyString + "'");
// }
// } else {
// Report that the incoming message was not of the expected
// type, and throw an exception
// System.out.println("Reply message was not a TextMessage");
// throw new JMSException("Retrieved the wrong type of message");
// }
// Remember to call the close() method on all of the objects
// used, to ensure proper clean-up of resources
// Closing QueueReceiver
// System.out.println("Closing QueueReceiver");
// queueReceiver.close();
// Closing QueueSender
System.out.println("Closing QueueSender");
queueSender.close();
// Closing QueueSesssion.
System.out.println("Closing Session");
session.close();
session = null;
// Closing QueueConnection.
System.out.println("Closing Connection");
connection.close();
connection = null;
} catch( JMSException je ) {
System.out.println("caught JMSException: " + je);
// check for a linked exception that provides more detail
Exception le = je.getLinkedException();
if (le != null) System.out.println("linked exception: "+le);
} catch( Exception e ) {
// This catches any exception thrown in the main body of
// the program, displaying it on screen
System.out.println("Caught exception: " + e );
} finally {
// A finally block is a good place to ensure that we don't forget
// to close the most important JMS objects
try {
if (session != null) {
System.out.println("closing session");
session.close();
}
if (connection != null) {
System.out.println("closing connection");
connection.close();
}
} catch (JMSException je) {
System.out.println("failed with "+je);
}
}
System.out.println("finished");
}
//=========================================================================
/** create an InitialDirContext for retrieving JNDI objects. */
static InitialDirContext initJNDI(String icf, String url)
throws JMSException, Exception
{
// the context to be returned from this routine
InitialDirContext ctx = null;
// hashtable for the JNDI initialisation properties
Hashtable environment = new Hashtable();
if( url==null ) {
// A URL must be supplied, so throw an exception if none is available
throw new Exception("You must specify the -url providerURL parameter");
}
// Firstly, store the name of the 'initial context factory'
environment.put(Context.INITIAL_CONTEXT_FACTORY, icf);
// Secondly, specify the location of the JNDI service provider
environment.put(Context.PROVIDER_URL, url);
// Finally, ensure that referrals are thrown. This is required
// as a workaround to a problem encountered with certain comb-
// inations of JNDI service provider and JNDI classes
environment.put(Context.REFERRAL, "throw");
try {
// Attempt to connect to the JNDI service provider, supply-
// ing the properties previously supplied
ctx = new InitialDirContext( environment );
} catch( Exception e ) {
// return an exception to the caller
JMSException je = new JMSException("Cannot create JNDI InitialContext!");
je.setLinkedException(e);
throw je;
}
return ctx;
}
//=========================================================================
/** Get the named QueueConnectionFactory object from the JNDI namespace.
*/
static QueueConnectionFactory getConnectionFactoryFromJNDI(Context ctx,
String name)
throws JMSException
{
// the factory to be returned
QueueConnectionFactory factory = null;
System.out.println( "Retrieving a QueueConnectionFactory from JNDI");
try {
// Attempt to retrieve a QueueConnectionFactory from the
// JNDI namespace
factory = (QueueConnectionFactory)ctx.lookup( name );
} catch( Exception e ) {
// If this has failed, try the operation again, this
// time prefixing the lookup key with "cn=". Such a
// prefix is required when using an LDAP provider
try {
factory = (QueueConnectionFactory)ctx.lookup( "cn="+name );
} catch( Exception e2 ) {
// After a second failure pass an exception to the caller
JMSException je =
new JMSException("Unable to retrieve the QCF object from JNDI");
je.setLinkedException(e2);
throw je;
}
}
return factory;
}
//=========================================================================
/** Get the named Queue object from the JNDI namespace. */
static Queue getQueueFromJNDI(Context ctx, String name)
throws JMSException
{
// The Queue to be returned
Queue ioQueue = null;
System.out.println("Retrieving a Queue from JNDI");
try {
// Attempt to retrieve a Queue from the JNDI namespace
ioQueue = (Queue)ctx.lookup( name );
} catch( Exception e ) {
try {
// If this fails, attempt again with the "cn=" prefix
ioQueue = (Queue)ctx.lookup( "cn="+name );
} catch( Exception e2 ) {
// After a second failure pass an exception back to the caller
JMSException je = new JMSException("Unable to retrieve the Queue '"+
name+"' from JNDI");
je.setLinkedException(e2);
throw je;
}
}
return ioQueue;
}
caught JMSException: javax.jms.JMSException: Cannot create JNDI InitialContext!
linked exception: javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory [Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory]
finished |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Jan 02, 2008 8:27 am Post subject: Re: Cannot create JNDI InitialContext! |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
ranjesh wrote: |
java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory |
_________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
fjb_saper |
Posted: Wed Jan 02, 2008 1:00 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
If you run outside of the app server you need the J2EE client footprint. Quite heavy for WAS.
Outside of the app server I prefer using the filecontext...
Enjoy  _________________ MQ & Broker admin |
|
Back to top |
|
 |
ling_71_99 |
Posted: Thu Jan 03, 2008 7:51 am Post subject: sibc_install-o0635.08.jar |
|
|
 Novice
Joined: 19 Nov 2007 Posts: 11 Location: Canada
|
|
Back to top |
|
 |
ranjesh |
Posted: Thu Jan 03, 2008 9:45 pm Post subject: |
|
|
Newbie
Joined: 31 Dec 2007 Posts: 4
|
Thnaks for the reply
Yes, i was trying to run out side application server host. how do i get J2EE client footprint or how do I use filecontext to resolve the above problem.
fjb_saper wrote: |
If you run outside of the app server you need the J2EE client footprint. Quite heavy for WAS.
Outside of the app server I prefer using the filecontext...
Enjoy  |
|
|
Back to top |
|
 |
ranjesh |
Posted: Thu Jan 03, 2008 9:47 pm Post subject: Re: sibc_install-o0635.08.jar |
|
|
Newbie
Joined: 31 Dec 2007 Posts: 4
|
Thnaks mike, i will try the steps and get back to you
|
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|