ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ Java / JMS » help me to run my first program in MQ

Post new topic  Reply to topic Goto page Previous  1, 2
 help me to run my first program in MQ « View previous topic :: View next topic » 
Author Message
jefflowrey
PostPosted: Fri Aug 19, 2005 3:22 am    Post subject: Reply with quote

Grand Poobah

Joined: 16 Oct 2002
Posts: 19981

If you're going to deploy your code to WAS, then you should not try to run your code outside WAS. This means either run it on a local test environment from WSAD/RAD or a development server of some kind or another.

It is very complicated to get all of the necessary jar files for establishing a connection to the WAS JDNI, without being inside a WAS environment.
_________________
I am *not* the model of the modern major general.
Back to top
View user's profile Send private message
mailtosathiyan@gmail.com
PostPosted: Sun Aug 21, 2005 10:29 pm    Post subject: i m able to run the code(ptpsample2) Reply with quote

Novice

Joined: 02 Aug 2005
Posts: 13

hi
with following option

INITIAL_CONTEXT_FACTORY=com.sun.jndi.fscontext.RefFSContextFactory
PROVIDER_URL=file:/C:/myjndi

the example code provided by IBM is working (ptpsample2.java)
but ptpsample1.java still giving problem while creating the sender

( i have created QCF and Q objects using JMSAdmin)
the error at this line
QueueSender queueSender = session.createSender(ioQueue);

getting
following error
javax.jms.InvalidDestinationException: MQJMS2008: failed to open MQ queue null
linked exception: com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2085

what could be the problem?
can any one explain me?
regards
sathiya
Back to top
View user's profile Send private message
jefflowrey
PostPosted: Mon Aug 22, 2005 3:25 am    Post subject: Reply with quote

Grand Poobah

Joined: 16 Oct 2002
Posts: 19981

The Reason Codes are fully documented in the manual called "Messages".
Messages wrote:
2085 (X'0825')MQRC_UNKNOWN_OBJECT_NAME
Explanation:
An MQOPEN or MQPUT1 call was issued, but the object identified by the ObjectName and ObjectQMgrName fields in the object descriptor MQOD cannot be found. One of the following applies:

The ObjectQMgrName field is one of the following:
Blank
The name of the local queue manager
The name of a local definition of a remote queue (a queue-manager alias) in which the RemoteQMgrName attribute is the name of the local queue manager
but no object with the specified ObjectName and ObjectType exists on the local queue manager.
The object being opened is a cluster queue that is hosted on a remote queue manager, but the local queue manager does not have a defined route to the remote queue manager.
The object being opened is a queue definition that has QSGDISP(GROUP). Such definitions cannot be used with the MQOPEN and MQPUT1 calls.
This can also occur in response to a command that specifies the name of an object or other item that does not exist.

Completion Code:
MQCC_FAILED

Programmer Response:
Specify a valid object name. Ensure that the name is padded to the right with blanks if necessary. If this is correct, check the object definitions.


So it is very likely that you are trying to open a queue with either the wrong name or a queue that does not exist. OR the sample program requires some queue manager setup that you didn't do - perhaps because it's not completely documented.
_________________
I am *not* the model of the modern major general.
Back to top
View user's profile Send private message
mailtosathiyan@gmail.com
PostPosted: Mon Aug 22, 2005 4:36 am    Post subject: getting error while compiling with jndi option Reply with quote

Novice

Joined: 02 Aug 2005
Posts: 13

hi
this is the example code getting error

//%PUB_START%
//Licensed Materials - Property of IBM
//
//5724-H72, 5655-L82, 5724-L26
//
//(c) Copyright IBM Corp. 1999, 2005
//
//US Government Users Restricted Rights - Use, duplication or
//disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//%PUB_END%
//@(#) jms/samples/En_US/PTPSample01.java, jms, j000, j000-L050121 1.5 05/01/20 12:09:50
/*
* Point to Point Sample01
*
* Demonstrates:
* how to retrieve administered objects from JNDI namespace
* how to create administered objects at runtime if no JNDI is availabe
* how to create connections, sessions, senders and receivers
* how to send and receive a message
*
* This program connects to the default queue manager on the local machine
* using the MQ Classes for Java bindings. It then sends a message to the
* queue SYSTEM.DEFAULT.LOCAL.QUEUE and reads it back again.
*
* This sample uses the same administered objects as the installation
* verification test program. These are:
*
* (i) an MQQueueConnectionFactory bound under the name "ivtQCF", created
* using no explicit properties, thus making use of all available
* defaults
*
* e.g. DEFINE QCF(ivtQCF)
*
* (ii) an MQQueue bound under the name "ivtQ", created using the property
* QUEUE(SYSTEM.DEFAULT.LOCAL.QUEUE) plus other available defaults
*
* e.g. DEFINE Q(ivtQ) QUEUE(SYSTEM.DEFAULT.LOCAL.QUEUE)
*
* This sample will fail if these objects cannot be retrieved from a JNDI
* service provider, unless -nojndi is specified (see below). They can be
* created automatically by running the IVTSETUP script, and can be auto-
* matically removed by running the IVTTIDY script.
*
* Usage:
*
* java PTPSample01 -nojndi [ -m qmgr ] [ -t ]
* or java PTPSample01 -url providerURL [ -icf initialContextFactory ] [ -t ]
*
* where:
*
* -nojndi - specify this to disable JNDI lookup of
* the two required administered objects
* -m qmgr - specify the name of the queue manager to
* connect to, if not the default one
* -t - enabled tracing
* -url providerURL - specify the URL of the JNDI initial
* context (this parameter is obligatory
* unless the -nojndi switch is supplied,
* in which case it is ignored
* -icf initialContextFactory - specify the initialContextFactory for
* JNDI [com.sun.jndi.ldap.LdapCtxFactory]
*
*/

//Import of Sun's JMS interface
package test1;
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 PTPSample01
{
// These are used when the -nojndi flag is given.
public static String QMGR = "QM1";
//public static final String QUEUE = "SYSTEM.DEFAULT.LOCAL.QUEUE" ;
public static final String QUEUE = "Q1" ;

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

public static void main( String[] args )
{
//System.out.println("prop...."+ System.getProperties());
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 url = null;

System.out.println( "\n5648-C60, 5724-B41, 5655-F10 (c) Copyright IBM Corp. 1999. " +
"All Rights Reserved.");
System.out.println( "Websphere MQ classes for Java(tm) Message Service - "+
"Point to Point Sample 1"+ "\n" );

// 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);
System.out.println("factory..."+factory);
} 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);
System.out.println("ioQueue..."+ioQueue.getQueueName());
} else {
// Use the session to create the queue object, supplying
// the required MQ-specific parameter
ioQueue = session.createQueue( QUEUE );
System.out.println("ioQueue..."+ioQueue.getQueueName());
}

// 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("1");
QueueSender queueSender = session.createSender(ioQueue);
System.out.println("2");

// 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);
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
//environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
// Secondly, specify the location of the JNDI service provider
//environment.put(Context.PROVIDER_URL, url);
environment.put(Context.PROVIDER_URL, "file:/C:/myjndi");
//environment.put(Context.PROVIDER_URL, "iiop://localhost:2809");

// 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;
}
System.out.println("ctx..."+ctx);
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
System.out.println("1");
factory = (QueueConnectionFactory)ctx.lookup( name );
System.out.println("2");

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

}

regards
sathiya
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Mon Aug 22, 2005 4:33 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

Well did you do the JNDI setup required for this to work ??

Enjoy
Back to top
View user's profile Send private message Send e-mail
mailtosathiyan@gmail.com
PostPosted: Mon Aug 22, 2005 9:01 pm    Post subject: Reply with quote

Novice

Joined: 02 Aug 2005
Posts: 13

hi
yes
but code is not working
throwing error as shown in the previous posts
regards
sathiya
Back to top
View user's profile Send private message
jefflowrey
PostPosted: Tue Aug 23, 2005 3:54 am    Post subject: Reply with quote

Grand Poobah

Joined: 16 Oct 2002
Posts: 19981

mailtosathiyan@gmail.com wrote:
hi
yes
but code is not working
throwing error as shown in the previous posts
regards
sathiya


It is not the code that is not working.

When you are using JNDI, then the Queue Destination you are requesting points to a queue that DOES NOT EXIST on the queue manager. Or when you have set the nojndi option, then the queue "Q1" DOES NOT EXIST on the queue manager.
_________________
I am *not* the model of the modern major general.
Back to top
View user's profile Send private message
mailtosathiyan@gmail.com
PostPosted: Tue Aug 23, 2005 8:04 pm    Post subject: i have solved this problem Reply with quote

Novice

Joined: 02 Aug 2005
Posts: 13

hi all
in JMSAdmin i have entered like this

def qcf(ivtQCF) qmgr(QM1)
def q(ivtQ) qmgr(QM1) queue(Q1)
now this code works fine
thanks for all

regards
sathiya
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Goto page Previous  1, 2 Page 2 of 2

MQSeries.net Forum Index » IBM MQ Java / JMS » help me to run my first program in MQ
Jump to:  



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
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.