1

我有一个 Play 应用程序(我使用的是 Play 2.1.0),我想从中调用在 GlassFish 服务器上运行的 EJB。因此,我尝试在 Play 应用程序中添加 gf-client.jar 作为依赖项,并以与在独立应用程序中相同的方式获取对 EJB 的引用:

Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
properties.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
properties.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
properties.put("org.omg.CORBA.ORBInitialHost", "localhost");
properties.put("org.omg.CORBA.ORBInitialPort", "3700");
properties.put("java.naming.provider.url", "iiop://localhost:3700");
Context ctx = new InitialContext(properties);
ctx.lookup("java:global/MyApplication/MyEJB/MyBean");

但是,执行查找时出现以下异常:

play.api.Application$$anon$1: Execution exception[[RuntimeException: javax.naming.NotContextException: global is not a subcontext]]
        at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.0]
        at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.0]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:132) [play_2.10.jar:2.1.0]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:128) [play_2.10.jar:2.1.0]
        at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.0]
        at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.0]
java.lang.RuntimeException: javax.naming.NotContextException: global is not a subcontext
        at play.libs.F$Promise$6.apply(F.java:401) ~[play_2.10.jar:2.1.0]
        at scala.concurrent.Future$$anonfun$map$1.liftedTree2$1(Future.scala:253) ~[scala-library.jar:na]
        at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
        at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29) ~[scala-library.jar:na]
        at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.liftedTree1$1(BatchingExecutor.scala:67) ~[akka-actor_2.10.jar:na]
Caused by: javax.naming.NotContextException: global is not a subcontext
        at tyrex.naming.EnvContext.internalLookup(Unknown Source) ~[tyrex.jar:1.0.1  November 11 2003 1703]
        at tyrex.naming.EnvContext.lookup(Unknown Source) ~[tyrex.jar:1.0.1  November 11 2003 1703]
        at tyrex.naming.java.JavaContext.lookup(Unknown Source) ~[tyrex.jar:1.0.1  November 11 2003 1703]
        at javax.naming.InitialContext.lookup(InitialContext.java:411) ~[na:1.7.0_10]
        at controllers.Application.index(Application.java:36) ~[na:na]
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(routes_routing.scala:77) ~[na:na]

具有相同 gf-client.jar 依赖项的完全相同的代码在独立应用程序中运行良好。我实际得到的是 NotContextException 并且在异常堆栈跟踪中我看到以下行

at tyrex.naming.EnvContext.lookup(Unknown Source)

让我相信由于某种原因,我得到的 InitialContext 不是“正确的”,因为如果我指定一个不存在的 JNDI 名称,我在独立应用程序中得到的堆栈跟踪有点不同:

Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/Test/MyBean' in SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: Test]
    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 com.eurodyn.testmavenjavaapp.App.main(App.java:24)
Caused by: javax.naming.NameNotFoundException: Test
    at com.sun.enterprise.naming.impl.TransientContext.resolveContext(TransientContext.java:310)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:218)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:219)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
    at com.sun.enterprise.naming.impl.RemoteSerialContextProviderImpl.lookup(RemoteSerialContextProviderImpl.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:144)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:174)
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1624)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1486)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:990)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:214)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:742)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:539)
    at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2324)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)

我究竟做错了什么?是否有其他方法可以从 Play 应用程序执行 JNDI 查找?我从这个 Stackoverflow 答案中了解到,这是可以做到的。

4

1 回答 1

0

我通过将 gf-client-module 添加到 project/Build.scala 中的依赖项而不是尝试直接链接 jar 来为我解决了这个问题(Play 2.1.3 和 Glassfish 3.1.2.2):

"org.glassfish.main.appclient.client" % "gf-client-module" % "3.1.2.2" 

在解决依赖关系时,它错过了我可以在这里找到的模块 org.eclipse.persistence#javax.persistence;2.0.4.v201112200901:

 resolvers += "opencastproject" at "http://repository.opencastproject.org/nexus/content/groups/public/" 

为了确定要查找的名称,我检查了该上下文中可用的名称:

InitialContext ic = getInitialContext();
System.out.println(ic.getEnvironment());
NamingEnumeration<NameClassPair> childList = ic.list("");
while (childList.hasMore()) {
NameClassPair ncPair = (NameClassPair) childList.next();
    if (ncPair.getName().contains("Job") == false) {
        System.out.println(ncPair.getName() + " (type " + ncPair.getClassName() + ")");
    }
}

我使用的名称与那里列出的名称完全相同。

问候

于 2013-09-23T13:30:48.470 回答