我有一个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,并且在异常stacktrace中我看到以下行
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查找?我从this Stackoverflow answer了解到这是可以做到的事情。
答案 0 :(得分:0)
我已经通过将gf-client-module添加到项目/ 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() + ")");
}
}
我使用的名字与那里列出的完全相同。
此致