?
Solved

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

Posted on 2007-10-05
4
Medium Priority
?
4,569 Views
Last Modified: 2008-01-09
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,



 
0
Comment
Question by:mmingfeilam
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Tomas Helgi Johannsson earned 750 total points
ID: 20033696
0
 

Author Comment

by:mmingfeilam
ID: 20035417
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

0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 20035752
Did you try the solutiion in the second link that I provided ?

Regards,
    Tomas Helgi
0
 

Author Comment

by:mmingfeilam
ID: 20043458
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) {}
            }
        }
    }
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question