轻松启动独立JNDI服务器(并注册一些资源)

Chr*_*her 9 java jndi datasource

出于测试目的,我正在寻找一种简单的方法来启动独立的 JNDI服务器,并以"java:/comp/env/jdbc/mydatasource"编程方式绑定我的javax.sql.DataSource .

服务器应该将自己绑定到某个URL,例如:"java.naming.provider.url = jnp:// localhost:1099"(不一定是JNP),这样我就可以从另一个进程查找我的数据源.我不关心我必须使用哪个JNDI服务器实现(但我不想启动一个成熟的JavaEE服务器).

这应该很容易,但令我惊讶的是,我找不到任何(工作)教程.

Tom*_*son 8

JDK包含RMI注册表JNDI提供程序.这意味着您可以将RMI注册表用作JNDI服务器.所以,只需启动rmir​​egistry,将java.naming.factory.initial设置为com.sun.jndi.rmi.registry.RegistryContextFactory,就可以了.

RMI注册表具有平面命名空间,因此您将无法绑定到java:/ comp/env/jdbc/mydatasource,但您将能够绑定到某些东西,因此它将接受java:/ comp/env/jdbc/mydatasource,但会将其视为单组件名称(谢谢,@ EJP).

我写了一个小应用程序来演示如何执行此操作:https://bitbucket.org/twic/jndiserver/src

我仍然不知道JNP服务器应该如何工作.

  • @EJP:我知道你的意思。但是在我看来,如果可以使用P客户端访问Q,那么Q可能*被描述为* P服务器。此外,克里斯使用了“ JNDI服务器”一词,并定义了对他意味着什么。如果有符合这个定义的东西,我们是谁通过告诉另一个人不是那个人践踏了那个男人? (2认同)

小智 5

我研究了约翰的代码,现在运行良好。

在这个版本中,我使用的是 JBoss5.1.0.GA 的库,参见下面的 jar 列表:

  • jboss-5.1.0.GA\client\jbossall-client.jar
  • jboss-5.1.0.GA\server\minimal\lib\jnpserver.jar
  • jboss-5.1.0.GA\server\minimal\lib\log4j.jar
  • jboss-remote-naming-1.0.1.Final.jar(从http://search.maven.com下载)

这是新代码:

import java.net.InetAddress;
import java.util.Hashtable;
import java.util.concurrent.Callable;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.jnp.server.Main;
import org.jnp.server.NamingBeanImpl;

public class StandaloneJNDIServer implements Callable<Object> {

public Object call() throws Exception {

    setup();

    return null;
}

@SuppressWarnings("unchecked")
private void setup() throws Exception {

    //configure the initial factory
    //**in John´s code we did not have this**
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

    //start the naming info bean
    final NamingBeanImpl _naming = new NamingBeanImpl();
    _naming.start();

    //start the jnp serve
    final Main _server = new Main();
    _server.setNamingInfo(_naming);
    _server.setPort(5400);
    _server.setBindAddress(InetAddress.getLocalHost().getHostName());
    _server.start();

    //configure the environment for initial context
    final Hashtable _properties = new Hashtable();
    _properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    _properties.put(Context.PROVIDER_URL,            "jnp://10.10.10.200:5400");

    //bind a name
    final Context _context = new InitialContext(_properties);
    _context.bind("jdbc", "myJDBC");

}

public static void main(String...args){

    try{

        new StandaloneJNDIServer().call();

    }catch(Exception _e){
        _e.printStackTrace();
    }

}
}
Run Code Online (Sandbox Code Playgroud)

要获得良好的日志记录,请使用此 log4j 属性:

log4j.rootLogger=TRACE, A1 
log4j.appender.A1=org.apache.log4j.ConsoleAppender 
log4j.appender.A1.layout=org.apache.log4j.PatternLayout 
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Run Code Online (Sandbox Code Playgroud)

要使用独立 JNDI 服务器,请使用此客户端类:

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * 
 * @author fabiojm - Fábio José de Moraes
 *
 */
public class Lookup {

public Lookup(){

}

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    final Hashtable _properties = new Hashtable();

    _properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    _properties.put("java.naming.provider.url",    "jnp://10.10.10.200:5400");

    try{
        final Context _context = new InitialContext(_properties);

        System.out.println(_context);
        System.out.println(_context.lookup("java:comp"));
        System.out.println(_context.lookup("java:jdbc"));

    }catch(Exception _e){
        _e.printStackTrace();
    }
}

}
Run Code Online (Sandbox Code Playgroud)