简体   繁体   中英

Unable to find JNDI resource for JMS Chat Application

I am trying to develop a JMS application that runs on Glassfish 3. Eclipse Indigo is my IDE. I have tried to run this example . If you find it necessary, I will copy the code here. I do not understand exactly what should I do with JNDI. I have created through the Admin Console the two needed resources with the exact specified names, but I get this exception:

java.lang.RuntimeException: Orb initialization erorr
at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:180)
at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:365)
at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:372)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:402)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at Chat.<init>(Chat.java:38)
at Chat.main(Chat.java:113)
Caused by: java.lang.NullPointerException
at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:152)
... 9 more
javax.naming.NamingException: Lookup failed for 'TopicConnectionFactory' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.lang.RuntimeException: Orb initialization erorr]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at Chat.<init>(Chat.java:38)
at Chat.main(Chat.java:113)
Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.lang.RuntimeException: Orb initialization erorr]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:352)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504)
... 4 more
Caused by: java.lang.RuntimeException: Orb initialization erorr
at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:180)
at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:365)
at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:372)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:402)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347)
... 5 more
Caused by: java.lang.NullPointerException
at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:152)
... 9 more

From what I have read, on a server, the InitialContext should work automatically.

What am I doing wrong?

Upon constructing the InitialContext you need to provide some properties that describes how to access and get objects from the JNDI server.

Mainly you need to provide a property that marks the vendor's implementation and another that points to the data store location, and you may need to specify other security or vendor specific properties.

In your case (glassfish), I think the following properties needs to be set:

// the initial context factory, choosing the glassfish implementation
env.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
// glassfish's server location
env.setProperty("org.omg.CORBA.ORBInitialHost", "<host name or IP>");
env.setProperty("org.omg.CORBA.ORBInitialPort", "<port number>"); // default is 3700  

I hope this code will helped you,my environment : JEE6+glassfish3V

 private static ConnectionFactory connectionFactory;
 private static Queue queue;
 public static void main(String[] args) throws NamingException {
      Connection connection = null;
      Session session = null;
      MessageConsumer consumer = null;
      TextMessage message = null;

      Properties env = new Properties();

      //glassfish3V
      env.put(Context.PROVIDER_URL, "iiop://localhost:8080");
      InitialContext jndi = new InitialContext(env);
      connectionFactory = (ConnectionFactory) jndi.lookup("jms/ConnectionFactory");
      queue = (Queue) jndi.lookup("jms/Queue"); // put your Queue here


      try {
           connection = connectionFactory.createConnection();
           session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           consumer = session.createConsumer(queue);
           connection.start();

           while (true) {
                Message m = consumer.receive(1);

                if (m != null) {
                     if (m instanceof TextMessage) {
                          message = (TextMessage) m;
                          System.out.println(
                                  "Reading message: " + message.getText());
                     } else {
                          break;
                     }
                }
           }
      } catch (JMSException e) {
           System.err.println("Exception occurred: " + e.toString());
      } finally {
           if (connection != null) {
                try {
                     connection.close();
                } catch (JMSException e) {
                }
           }
      }
 }

It turned out to be a JAR problem with my classpath. The needed JARs are the one specified in the question, and the other two in the accepted answer here , namely: gf-client-module.jar , imqjmsra.jar and imqbroker.jar .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM