无法找到JMS聊天应用程序的JNDI资源

时间:2012-03-04 16:08:25

标签: java jms jndi glassfish-3

我正在尝试开发一个在Glassfish 3上运行的JMS应用程序。 Eclipse Indigo是我的IDE。 我试图运行 this example 。如果您觉得有必要,我会在这里复制代码。 我不明白我应该怎么做JNDI。我通过管理控制台创建了具有确切指定名称的两个所需资源,但是我得到了这个例外:

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

根据我的阅读,在服务器上,InitialContext应该自动运行。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

在构造InitialContext时,您需要提供一些描述如何从JNDI服务器访问和获取对象的属性。

主要是您需要提供一个标记供应商实现的属性,另一个指向数据存储位置的属性,您可能需要指定其他安全性或供应商特定的属性。

在你的情况下(glassfish),我认为需要设置以下属性:

// 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  

答案 1 :(得分:0)

结果是我的类路径的JAR问题。所需的JAR是问题中指定的JAR,另外两个是接受的答案 here ,即:gf-client-module.jarimqjmsra.jar和{{1} }。

答案 2 :(得分:0)

我希望这段代码能帮到你,我的环境: 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) {
                }
           }
      }
 }