2013-04-04 41 views
1

我有一個Play應用程序(我使用的是Play 2.1.0),我想從中調用GlassFish服務器上運行的EJB。因此,我曾嘗試加入GF-client.jar的在我的Play應用程序的依賴關係,並得到一個參考的EJB以同樣的方式我會在一個獨立的應用程序做:來自Play應用程序的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查找?我從this Stackoverflow answer瞭解到這是可以完成的事情。

+0

GF-client.jar中是指許多其他的罐子所以最好是參考GlassFish的目錄,以便獲得所有必需的罐子。 GlassFish在日誌中輸出EJB JNDI名稱,請檢查日誌中的名稱 – 2013-04-04 12:58:35

+0

@Abdullah Shaikh這是否可以在Play應用程序中使用?據我所知,添加依賴項的唯一方法是從Maven倉庫(我使用過的方法)或通過將jar複製到我的應用程序的lib文件夾中,這意味着我必須將jar從原始無論如何。如果我在另一臺機器上運行遠程GlassFish,該怎麼辦?在這種情況下,我甚至無法訪問GlassFish安裝目錄。 – Christina 2013-04-04 13:02:05

+0

您的查找名稱爲「java:global/MyBean」,它的格式應該是java:global/your application name/ejb module/bean name或java:global/ejb module/bean name incase ejb module is outside ears – 2013-04-04 13:07:16

回答

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

可以肯定什麼樣的名字lookup我檢查了在這種情況下可用的名稱:

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() + ")"); 
    } 
} 

我完全按照列出的名稱使用名稱。

問候

相關問題