Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

InvalidDestinationException: MQJMS2008: failed to open MQ queue

843830Feb 11 2004 — edited Dec 20 2007
Hi,

I got a jms exception in my application . Description of the error as folows

caught JMSException: javax.jms.InvalidDestinationException: MQJMS2008: failed to open MQ queue
javax.jms.InvalidDestinationException: MQJMS2008: failed to open MQ queue
at com.ibm.mq.jms.MQQueueSession.getQueueOpenException(MQQueueSession.java:925)
at com.ibm.mq.jms.MQQueueSession.getOutputQueue(MQQueueSession.java:866)
at com.ibm.mq.jms.MQQueueSession.createSender(MQQueueSession.java:206)
at Sample.main(Sample.java:76)
linked exception: com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2085

here is the code snippet. please help me.
thanks in advance,

Sridhar


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.MQQueueConnectionFactory;

// Import required for program tracing
import com.ibm.mq.jms.services.ConfigEnvironment;

public class Sample
{
// These are used when the -nojndi flag is given.
public static String QMGR = "IMedia.queue.manager";
public static final String QUEUE = "IMedia" ;

// The following can be modified if you want to experiment with
// using other administered objects with different content.
public static final String qcfLookup = "jms/IMediaQcf";
public static final String qLookup = "jms/IMediaQueue";

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.fscontext.RefFSContextFactory";
String url = "file:///D:/iMedia";

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

// obtain the initial context
ctx = initJNDI(icf, url);

// get the connection factory from JNDI
factory = getConnectionFactoryFromJNDI(ctx, qcfLookup);

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 from JNDI
ioQueue = getQueueFromJNDI(ctx, qLookup);

// 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");
System.out.println( "Q name is " + ioQueue.getQueueName() );
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);
je.printStackTrace();
// 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();


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

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 17 2008
Added on Feb 11 2004
5 comments
3,962 views