testing - 无法访问远程 EJB

标签 testing glassfish ejb-3.0 integration jndi

我已经创建了带有远程和本地接口(interface)的 EJB。

@Stateless
public class VendorBean implements VendorBeanLocal, VendorBeanRemote {

    ...

}

@Local
public interface VendorBeanLocal {

    ...

}

@Remote
public interface VendorBeanRemote {

   ...

}

有一些方法可以代替点(在这种情况下无关紧要)。

然后将其部署到 glassfish 3.1。它可以从我的客户端 Web 应用程序(在同一台服务器上部署为 war )访问并且工作正常。但我无法从我的集成测试中访问它。

我编写了应该由 maven 2 运行的集成测试。我在 glassfish server.log 中查看了这个 bean 的 JNDI 名称:

java:global/<my business module name>/VendorBean!<package>.VendorBeanRemote

并编写了测试。但我总是在查找时遇到异常:

javax.naming.NameNotFoundException [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
    at com.sun.jndi.cosnaming.ExceptionMapper.mapException(ExceptionMapper.java:61)
    at com.sun.jndi.cosnaming.CNCtx.callResolve(CNCtx.java:501)
    at com.sun.jndi.cosnaming.CNCtx.lookup(CNCtx.java:540)
    at com.sun.jndi.cosnaming.CNCtx.lookup(CNCtx.java:518)
    at javax.naming.InitialContext.lookup(InitialContext.java:409)
    at com.widewebtech.mercury.core.test.ejb.VendorBeanIntegrationTest.manageVendor(VendorBeanIntegrationTest.java:126)
    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:616)
    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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    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:616)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
    at org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(NotFoundHelper.java:72)
    at org.omg.CosNaming._NamingContextExtStub.resolve(_NamingContextExtStub.java:406)
    at com.sun.jndi.cosnaming.CNCtx.callResolve(CNCtx.java:487)
    ... 33 more

测试代码:

Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
p.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
p.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context context = new InitialContext(p);

Object o = context.lookup("java:global/<my business module name>/VendorBean!<package>.VendorBeanRemote");

我很困惑。我读了很多文章,尝试了很多建议,但没有任何效果。

我还尝试在测试中列出上下文:

NamingEnumeration<NameClassPair> list = context.list(""); 
while (list.hasMore()) { 
    NameClassPair ncPair = (NameClassPair) list.next(); 
    System.out.print(ncPair.getName() + " (type "); 
    System.out.println(ncPair.getClassName() + ")"); 
}

结果:

<package>.VendorBeanRemote__3_x_Internal_RemoteBusinessHome__ (type com.sun.corba.se.impl.corba.CORBAObjectImpl)
SerialContextProvider (type com.sun.corba.se.impl.corba.CORBAObjectImpl)
java:global (type com.sun.jndi.cosnaming.CNCtx)
INITIAL_GIS (type com.sun.corba.se.impl.corba.CORBAObjectImpl)

因此,在上下文中看起来像 bean。

我做错了什么?请帮忙!

最佳答案

最终我设法解决了这个问题。所以,代码是:

Properties p = new Properties();
p.setProperty(Context.PROVIDER_URL, "corbaname:iiop:localhost:3700");
context = new InitialContext(p);

VendorBeanRemote vendorBean = (VendorBeanRemote) context.lookup("java:global/<my business module name>/VendorBean!<package>.VendorBeanRemote");

重要的是,gf-client.jar 必须在类路径中。它可以从 glassfish 库目录。

关于testing - 无法访问远程 EJB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9095608/

相关文章:

ruby-on-rails - 从 rspec 读取 rails env 变量

iphone - 在 iPhone 上测试 iPhone 应用程序是否有任何硬件要求

java - 如何通过远程接口(interface)代理访问有状态 session bean?

java - 带有 CMT 的 Ejb3 无状态 bean

ruby-on-rails - 使用 RSpec 测试 Rails 模型验证,无需测试 AR 本身

java - 自定义对象在 Jax-RS 中反序列化很好,但如果它在另一个对象中使用,则它不起作用

java - 如何配置 GlassFish 日志记录以在时间戳中显示毫秒?

java - Glassfish 项目似乎没有在 Tomcat 服务器上运行

java - 组件上的 EJB 调用因 java.security.AccessControlException 失败

java - 使用 DBUnit 测试数据库时如何忽略表的现有元素