Link to home
Start Free TrialLog in
Avatar of Michael Lam
Michael Lam

asked on

JNDI API lookup failed: javax.naming.NameNotFoundException: QueueConnectionFactory1

hi,

i m running sun's JMS tutorial,

http://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/client.html#1

027210. when i do this, i get the following:

C:\Projects\Leader\prototype\jms\PTPClient>java

-Djava.naming.factory.initial=co
m.sun.jndi.fscontext.RefFSContextFactory SimpleQueueSender MyQueue 3
Queue name is MyQueue
JNDI API lookup failed: javax.naming.NameNotFoundException:

QueueConnectionFactory1

i have added MyQueue and QueueConnectionFactory1:

JNDI Name: QueueConnectionFactory1
A unique identifier
Pool Name:QueueConnectionFactory1
Type:javax.jms.QueueConnectionFactory

and MyQueue:
JNDI Name:MyQueue
Required Field Type:javax.jms.Queue

and my class path:
.;%JMQ_HOME%\lib\jndi.jar;C:\Sun\AppServer\lib\install\applications\j

msra\imqjmsra.jar;%J2EE_HOME%\imq\lib\imq.jar;%J2EE_HOME%\lib\j2ee.ja

r;%J2EE_HOME%\lib\locale;C:\Sun\MessageQueue\lib;

i can't figure out what's wrong, most of the people who asked similar

questions online don't use the sun java message queue (JMQ), so their

solutions don't really work for me as they require different jar's

and different setup.


thanks,



 
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Lam
Michael Lam

ASKER

sorry, tried that, but got the same error.  here is the code, which i got from sun:

/**
 * The SimpleQueueSender class consists only of a main method,
 * which sends several messages to a queue.
 *
 * Run this program in conjunction with SimpleQueueReceiver.
 * Specify a queue name on the command line when you run the
 * program.  By default, the program sends one message.  Specify
 * a number after the queue name to send that number of messages.
 */
import javax.jms.*;
import javax.naming.*;

public class SimpleQueueSender {

    /**
     * Main method.
     *
     * @param args     the queue used by the example and,
     *                 optionally, the number of messages to send
     */
    public static void main(String[] args) {
        String                  queueName = null;
        Context                 jndiContext = null;
        QueueConnectionFactory  queueConnectionFactory = null;
        QueueConnection         queueConnection = null;
        QueueSession            queueSession = null;
        Queue                   queue = null;
        QueueSender             queueSender = null;
        TextMessage             message = null;
        final int               NUM_MSGS;
       
        if ( (args.length < 1) || (args.length > 2) ) {
            System.out.println("Usage: java SimpleQueueSender " +
                "<queue-name> [<number-of-messages>]");
            System.exit(1);
        }
        queueName = new String(args[0]);
        System.out.println("Queue name is " + queueName);
        if (args.length == 2){
            NUM_MSGS = (new Integer(args[1])).intValue();
        } else {
            NUM_MSGS = 1;
        }
       
        /*
         * Create a JNDI API InitialContext object if none exists
         * yet.
         */
        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API " +
                "context: " + e.toString());
            System.exit(1);
        }
       
        /*
         * Look up connection factory and queue.  If either does
         * not exist, exit.
         */
        try {
            queueConnectionFactory = (QueueConnectionFactory)
                jndiContext.lookup("QueueConnectionFactory1");
            queue = (Queue) jndiContext.lookup(queueName);
        } catch (NamingException e) {
            System.out.println("JNDI API lookup failed: " +
                e.toString());
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create sender and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
        try {
            queueConnection =
                queueConnectionFactory.createQueueConnection();
            queueSession =
                queueConnection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            queueSender = queueSession.createSender(queue);
            message = queueSession.createTextMessage();
            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText("This is message " + (i + 1));
                System.out.println("Sending message: " +
                    message.getText());
                queueSender.send(message);
            }

            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            queueSender.send(queueSession.createMessage());
        } catch (JMSException e) {
            System.out.println("Exception occurred: " +
                e.toString());
        } finally {
            if (queueConnection != null) {
                try {
                    queueConnection.close();
                } catch (JMSException e) {}
            }
        }
    }
}

and this is the config.bat file for creating the various jms objects:
@echo off

if "%JMQ_HOME%\lib\imq.jar" == "\lib\imq.jar" goto nojmqhome

REM  Create and add the queue
call "%JMQ_HOME%\bin\imqobjmgr" add -t q -l "MyQueue" -j "java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory" -j "java.naming.provider.url=file://C:/temp"

REM  Create and add the queue connection factory
call "%JMQ_HOME%\bin\imqobjmgr" add -t qf -l "QueueConnectionFactory1" -j "java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory" -j "java.naming.provider.url=file://C:/temp"

goto end

:noimqhome
      echo Please set the JMQ_HOME environment variable.
      goto end
:end

Did you try the solutiion in the second link that I provided ?

Regards,
    Tomas Helgi
i figured it out, i manually set the Properties for Context.  i believe you can do it using a properties file, but i couldn't get it to work that way, mainly because my program couldn't find com.sun.jndi.fscontext.RefFSContextFactory in the properties file.

import javax.jms.*;
import javax.naming.*;
import java.util.Properties;

public class SimpleQueueSender {
      //Initial context factory
      public static final String CTX_FACT = "com.sun.jndi.fscontext.RefFSContextFactory";
      //Provider URL
      public static final String PROV_URL = "file:C:\\temp";
      //JNDI name for the queue connection factory
      public static final String QCF_NAME = "QCFactory";
      //JNDI name for the topic connection factory
      public static final String TCF_NAME = "TCFactory";
      //JNDI name for the queue
      public static final String QUEUE_NAME = "myQueue";
      //JNDI name for the topic      
      public static final String TOPIC_NAME = "myTopic";

      public static class ExListener
      implements MessageListener
      {
            public void onMessage(Message msg)
            {
                  //done.release();
                  TextMessage message = (TextMessage)msg;
                  try
                  {
                        System.out.println("Reading message back: " +
                              message.getText());
                  }
                  catch (Throwable t)
                  {
                        t.printStackTrace();
                  }
            }
      }

    /**
     * Main method.
     *
     * @param args     the queue used by the example and,
     *                 optionally, the number of messages to send
     */
    public static void main(String[] args) {
        String                  queueName = null;
        Context                 jndiContext = null;
            QueueConnectionFactory queueConnectionFactory = null;
            javax.jms.QueueConnection queueConnection = null;
            javax.jms.QueueSession queueSession = null;
            javax.jms.Queue queue = null;
            javax.jms.QueueSender queueSender = null;
            javax.jms.QueueReceiver queueReceiver = null;
        TextMessage             message = null;
        final int               NUM_MSGS;
       
        if ( (args.length < 1) || (args.length > 2) ) {
            System.out.println("Usage: java SimpleQueueSender " +
                "<queue-name> [<number-of-messages>]");
            System.exit(1);
        }
        queueName = new String(args[0]);
        System.out.println("Queue name is " + queueName);
        if (args.length == 2){
            NUM_MSGS = (new Integer(args[1])).intValue();
        } else {
            NUM_MSGS = 1;
        }
       
        /*
         * Create a JNDI API InitialContext object if none exists
         * yet.
         */
        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API " +
                "context: " + e.toString());
            System.exit(1);
        }
       
        /*
         * Look up connection factory and queue.  If either does
         * not exist, exit.
         */
        try {
                  /*queueConnectionFactory = (javax.jms.QueueConnectionFactory)
                jndiContext.lookup("jms/QueueConnectionFactory");
                  queue = (javax.jms.Queue)jndiContext.lookup(queueName);*/

                  Properties prop = new Properties();
                  //Add the initial context factory
                  prop.put(Context.INITIAL_CONTEXT_FACTORY, CTX_FACT);
                  //Add the provider URL
                  prop.put(Context.PROVIDER_URL, PROV_URL);
                  //Create the initial context
                  Context ctx = new InitialContext(prop);

                  queueConnectionFactory =
                        (QueueConnectionFactory)ctx.lookup("QCFactory");

                  queue = (javax.jms.Queue)ctx.lookup(queueName);

        } catch (NamingException e) {
            System.out.println("JNDI API lookup failed: " +
                e.toString());
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create sender and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
        try {

                  
            queueConnection =
                queueConnectionFactory.createQueueConnection();
            queueSession =
                queueConnection.createQueueSession(false,
                    Session.AUTO_ACKNOWLEDGE);

                  BytesMessage bytesMessage = queueSession.createBytesMessage();

            queueSender = queueSession.createSender(queue);
            message = queueSession.createTextMessage();
            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText("This is message " + (i + 1));
                        bytesMessage.setStringProperty("messsage", "This is message");
                System.out.println("Sending message: " +
                    message.getText());
                        /*System.out.println("Sending byte message: " +
                              bytesMessage.toString());*/
                        try
                        {
                              queueSender.send(message);
                              // Set the async listener
                              queueReceiver = queueSession.createReceiver(queue);
                              queueReceiver.setMessageListener(new ExListener());

                        }
                        catch (JMSException e)
                        {
                              System.out.println("Exception occurred in for loop: " +
                                    e.toString());
                        }
                              /*(bytesMessage,
                        DeliveryMode.PERSISTENT,
                        Message.DEFAULT_PRIORITY,
                        Message.DEFAULT_TIME_TO_LIVE);*/
            }

            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            //queueSender.send(queueSession.createMessage());
        } catch (JMSException e) {
            System.out.println("Exception occurred: " +
                e.toString());
        } finally {
            if (queueConnection != null) {
                try {
                    queueConnection.close();
                } catch (JMSException e) {}
            }
        }
    }
}