项目使用Hibernate并部署在Jboss 5.1上。 Hibernate 使用 JNDI 来获取数据源。
我想为 DAO 层创建 JUnit 测试,为此我需要创建 JNDI 数据源和事务管理器以在不运行 Jboss 的情况下进行测试。
为此我编写了代码:
System.out.println(LocateRegistry.getRegistry());
Registry reg = LocateRegistry.createRegistry(1099);
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
properties.put(Context.PROVIDER_URL, "localhost:1099");
InitialContext initialContextcontext = new InitialContext(properties);
当我尝试查找或绑定(bind)命名对象时,应用程序很困惑。一段时间后抛出超时异常。绑定(bind)子上下文抛出相同的异常
在 Maven 中我包含依赖项:
<groupId>org.jboss.client</groupId>
<artifactId>jbossall-client</artifactId>
<version>4.2.3.GA</version>
经过研究,我遇到了这篇文章http://something-about-tech.blogspot.com/2008/12/injecting-jndi-datasource-for-junit.html 。但是这样的行 Registry reg = LocateRegistry.createRegistry(1099);
被错过了..我很困惑......
在相关帖子(Registering MySQL DataSource with JNDI for Hibernate)中,我对注册表数据源感兴趣,但有
Context.INITIAL_CONTEXT_FACTORY="com.sun.jndi.rmi.registry.RegistryContextFactory"
请帮忙。
最佳答案
org.jnp.interfaces.NamingContextFactory
是 JBoss 特定协议(protocol),您不能使用它连接到 RMI 注册表。试试这个测试
public class Test1 implements Serializable, Remote {
public static void main(String[] args) throws Exception {
Registry reg = LocateRegistry.createRegistry(1099);
Context ctx = new InitialContext();
ctx.bind("rmi://localhost/xxx", new Test1());
Test1 t1 = (Test1) ctx.lookup("rmi://localhost/xxx");
System.out.println(t1);
}
}
关于java - 如何为 Jboss 环境的 JUnit 注册 JNDI 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16558230/