2012-01-31 95 views
2

如何遠程訪問某個方法,或者我在做什麼錯誤?你能告訴我嗎?:無法使用JNDI訪問遠程EJB接口

public class OfferManagerBeanTest { 
    private static Context ctx; 

    @BeforeClass 
    public static void initContainer() throws Exception 
    { 
     Properties props = new Properties(); 
     props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory"); 
     props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming"); 
     props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"); 
     props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost"); 
     props.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); 

     ctx = new InitialContext(props); 
    } 

    @Test 
    public void commitOfferTest() throws NamingException 
    { 
     OfferManager offerManager = (OfferManager) ctx.lookup("java:global/project/projectEJB/OfferManagerBean"); 
     assertNotNull(offerManager);  
    } 
} 

我的問題是我運行這個測試用例後出現錯誤。

我得到它的工作,但我不明白爲什麼它在另一種情況下工作。

這裏是我的EJB:

@Stateless 
public class OfferManagerBean implements OfferManager, OfferManagerLocal 

OfferManager@Remote接口和OfferManagerLocal@Local接口。 如果我在我的WebProject(war文件)運行此,它正在...(我沒有在那裏的屬性)

如果我讓一切都像這樣,運行JUnit測試我得到一個錯誤,如:

javax.naming.NamingException: Lookup failed for 'java:global/project/projectEJB/OfferManagerBean' 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: OfferManagerBean not found] 
    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 ro.project.ejb.test.OfferManagerBeanTest.commitOfferTest(OfferManagerBeanTest.java:50) 
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: javax.naming.NameNotFoundException: OfferManagerBean not found 
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:219) 
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:219) 
    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:94) 
    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) 

我發現是這樣的:

ctx.lookup("java:global/project/projectEJB/OfferManagerBean!ro.project.ejb.interfaces.OfferManager"); 

?沒有更好/更短的解決方案嗎? 有誰能告訴我爲什麼會發生這種情況?

如果我讓我的宣言一樣:

@Stateless 
public class OfferManagerBean implements OfferManager 

一切工作。 你知道我該如何解決這個問題?

+0

我沒有明確地回答你的問題,但是,一般來說,你不應該在你的類中實現本地和遠程接口。遠程方法將按值傳遞,而本地將通過引用傳遞。你必須謹慎管理這一點。 – Preston 2012-01-31 16:54:36

回答

2

您可以使用代碼訪問EJB:

OfferManager offerManager = (OfferManager) 
        ctx.lookup("java:global/project/projectEJB/OfferManagerBean"); 

只有如果你的EJB實現一個接口。在這種情況下,很明顯你感興趣的是哪個界面(只有一個,所以 - 這裏沒有什麼魔法)。但是,如果您的EJB實現了多個接口,那麼當您通過JNDI訪問它時,您應該指定在什麼接口(視圖)中您感興趣。

因此在第二章中定義EJB 3.1規範FR的4.4全局JNDI訪問必須使用完整路徑:

java:global[/<app-name>]/<module-name>/<bean-name>[!<fully-qualified-intf-name>] 

或使用java:appjava:module如說明書中定義。