簡體   English   中英

如何為Jboss環境注冊JUnit的JNDI數據源

[英]How registry JNDI datasource for JUnit for Jboss environment

項目使用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);

當我嘗試查找或綁定命名的對象應用程序時會感到毛躁。 並在一段時間后拋出超時異常。 綁定子上下文會引發相同的異常

在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); 錯過了..我很困惑...

在相關的文章( 使用JNDI為Hibernate注冊MySQL數據源 )中,我對注冊表數據源感興趣,但是有一個

Context.INITIAL_CONTEXT_FACTORY="com.sun.jndi.rmi.registry.RegistryContextFactory"

請幫忙。

org.jnp.interfaces.NamingContextFactory是JBoss特定的協議,您不能使用它來連接到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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM