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\prototy pe\jms\PTP Client>jav a
-Djava.naming.factory.init ial=co
m.sun.jndi.fscontext.RefFS ContextFac tory SimpleQueueSender MyQueue 3
Queue name is MyQueue
JNDI API lookup failed: javax.naming.NameNotFoundE xception:
QueueConnectionFactory1
i have added MyQueue and QueueConnectionFactory1:
JNDI Name: QueueConnectionFactory1
A unique identifier
Pool Name:QueueConnectionFactor y1
Type:javax.jms.QueueConnec tionFactor y
and MyQueue:
JNDI Name:MyQueue
Required Field Type:javax.jms.Queue
and my class path:
.;%JMQ_HOME%\lib\jndi.jar; C:\Sun\App Server\lib \install\a pplication s\j
msra\imqjmsra.jar;%J2EE_HO ME%\imq\li b\imq.jar; %J2EE_HOME %\lib\j2ee .ja
r;%J2EE_HOME%\lib\locale;C :\Sun\Mess ageQueue\l ib;
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,
Â
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\prototy
-Djava.naming.factory.init
m.sun.jndi.fscontext.RefFS
Queue name is MyQueue
JNDI API lookup failed: javax.naming.NameNotFoundE
QueueConnectionFactory1
i have added MyQueue and QueueConnectionFactory1:
JNDI Name: QueueConnectionFactory1
A unique identifier
Pool Name:QueueConnectionFactor
Type:javax.jms.QueueConnec
and MyQueue:
JNDI Name:MyQueue
Required Field Type:javax.jms.Queue
and my class path:
.;%JMQ_HOME%\lib\jndi.jar;
msra\imqjmsra.jar;%J2EE_HO
r;%J2EE_HOME%\lib\locale;C
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Did you try the solutiion in the second link that I provided ?
Regards,
  Tomas Helgi
Regards,
  Tomas Helgi
ASKER
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.Ref FSContextF actory 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.Re fFSContext Factory";
      //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("Readin g 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.QueueConnection Factory)
        jndiContext.lookup("jms/Qu eueConnect ionFactory ");
                 queue = (javax.jms.Queue)jndiConte xt.lookup( queueName) ;*/
                 Properties prop = new Properties();
                 //Add the initial context factory
                 prop.put(Context.INITIAL_C ONTEXT_FAC TORY, CTX_FACT);
                 //Add the provider URL
                 prop.put(Context.PROVIDER_ URL, PROV_URL);
                 //Create the initial context
                 Context ctx = new InitialContext(prop);
                 queueConnectionFactory =
                      (QueueConnectionFactory)ct x.lookup(" QCFactory" );
                 queue = (javax.jms.Queue)ctx.looku p(queueNam e);
    } 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.cre ateQueueCo nnection() ;
      queueSession =
        queueConnection.createQueu eSession(f alse,
          Session.AUTO_ACKNOWLEDGE);
                 BytesMessage bytesMessage = queueSession.createBytesMe ssage();
      queueSender = queueSession.createSender( queue);
      message = queueSession.createTextMes sage();
      for (int i = 0; i < NUM_MSGS; i++) {
        message.setText("This is message " + (i + 1));
                      bytesMessage.setStringProp erty("mess sage", "This is message");
        System.out.println("Sendin g message: " +
          message.getText());
                      /*System.out.println("Send ing byte message: " +
                            bytesMessage.toString());* /
                      try
                      {
                            queueSender.send(message);
                            // Set the async listener
                            queueReceiver = queueSession.createReceive r(queue);
                            queueReceiver.setMessageLi stener(new ExListener());
                      }
                      catch (JMSException e)
                      {
                            System.out.println("Except ion occurred in for loop: " +
                                 e.toString());
                      }
                            /*(bytesMessage,
                      DeliveryMode.PERSISTENT,
                      Message.DEFAULT_PRIORITY,
                      Message.DEFAULT_TIME_TO_LI VE);*/
      }
      /*
       * Send a non-text control message indicating end of
       * messages.
       */
      //queueSender.send(queueSe ssion.crea teMessage( ));
    } catch (JMSException e) {
      System.out.println("Except ion occurred: " +
        e.toString());
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {}
      }
    }
  }
}
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.Re
      //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("Readin
                            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:
        "<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.QueueConnection
        jndiContext.lookup("jms/Qu
                 queue = (javax.jms.Queue)jndiConte
                 Properties prop = new Properties();
                 //Add the initial context factory
                 prop.put(Context.INITIAL_C
                 //Add the provider URL
                 prop.put(Context.PROVIDER_
                 //Create the initial context
                 Context ctx = new InitialContext(prop);
                 queueConnectionFactory =
                      (QueueConnectionFactory)ct
                 queue = (javax.jms.Queue)ctx.looku
    } 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.cre
      queueSession =
        queueConnection.createQueu
          Session.AUTO_ACKNOWLEDGE);
                 BytesMessage bytesMessage = queueSession.createBytesMe
      queueSender = queueSession.createSender(
      message = queueSession.createTextMes
      for (int i = 0; i < NUM_MSGS; i++) {
        message.setText("This is message " + (i + 1));
                      bytesMessage.setStringProp
        System.out.println("Sendin
          message.getText());
                      /*System.out.println("Send
                            bytesMessage.toString());*
                      try
                      {
                            queueSender.send(message);
                            // Set the async listener
                            queueReceiver = queueSession.createReceive
                            queueReceiver.setMessageLi
                      }
                      catch (JMSException e)
                      {
                            System.out.println("Except
                                 e.toString());
                      }
                            /*(bytesMessage,
                      DeliveryMode.PERSISTENT,
                      Message.DEFAULT_PRIORITY,
                      Message.DEFAULT_TIME_TO_LI
      }
      /*
       * Send a non-text control message indicating end of
       * messages.
       */
      //queueSender.send(queueSe
    } catch (JMSException e) {
      System.out.println("Except
        e.toString());
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {}
      }
    }
  }
}
ASKER
/**
 * 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:
        "<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("QueueC
      queue = (Queue) jndiContext.lookup(queueNa
    } 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.cre
      queueSession =
        queueConnection.createQueu
          Session.AUTO_ACKNOWLEDGE);
      queueSender = queueSession.createSender(
      message = queueSession.createTextMes
      for (int i = 0; i < NUM_MSGS; i++) {
        message.setText("This is message " + (i + 1));
        System.out.println("Sendin
          message.getText());
        queueSender.send(message);
      }
      /*
       * Send a non-text control message indicating end of
       * messages.
       */
      queueSender.send(queueSess
    } catch (JMSException e) {
      System.out.println("Except
        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"
REM Â Create and add the queue connection factory
call "%JMQ_HOME%\bin\imqobjmgr"
goto end
:noimqhome
      echo Please set the JMQ_HOME environment variable.
      goto end
:end