JNDI 代码实例

JNDI其实都是服务器自动绑定的,如websphere的datasource setup,在console里设好后,就自动和上下文绑定了。无需手写。

但是这次我们要作深入了解就手写下。
首先要有个中间件服务器,例如weblogic,webshpere.我就是用jboss做为服务器,免费,size小嘛。

首先启动jboss,然后就不管它。
我们打开ecplispe,在自己src java 的根目录下放下这个jndi.properties文件。
引用

# DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
#
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099



然后我们就开始写我们的java类了。

import java.io.FileInputStream;
import java.util.Properties;

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

public class JNDITest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Properties env = new Properties();
			// 载入jboss的SPI相关参数,包括初始上下文工厂,服务URL,等等
			env.load(new FileInputStream("jndi.properties"));
			env.list(System.out);
			// 通过JNDI api 初始化上下文
			InitialContext ctx = new javax.naming.InitialContext(env);
//			System.out.println("Got context");
//			// create a subContext
			ctx.createSubcontext("/sylilzy");
			ctx.createSubcontext("sylilzy/sily");
//			// rebind a object
			ctx.rebind("sylilzy/sily/a", "I am sily a!");
			ctx.rebind("sylilzy/sily/b", "I am sily b!");
			// lookup context
			Context ctx1 = (Context) ctx.lookup("sylilzy");
			Context ctx2 = (Context) ctx1.lookup("/sylilzy/sily");
			ctx2.bind("/sylilzy/g", "this is g");
			// lookup binded object
			Object o;
			o = ctx1.lookup("sily/a");
			System.out.println("get object from jndi:" + o);
			// rename the object
			ctx2.rename("/sylilzy/g", "g1");
			o = ctx2.lookup("g1");
			System.out.println("get object from jndi:" + o);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


然后会输出:

引用
-- listing properties --
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
get object from jndi:I am sily a!
get object from jndi:this is g


但是我还是再想运行一次。

引用
-- listing properties --
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
javax.naming.NameAlreadyBoundException; remaining name 'sylilzy'
at org.jnp.server.NamingServer.createSubcontext(NamingServer.java:465)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at org.jnp.server.NamingServer_Stub.createSubcontext(Unknown Source)
at org.jnp.interfaces.NamingContext.createSubcontext(NamingContext.java:942)
at org.jnp.interfaces.NamingContext.createSubcontext(NamingContext.java:924)
at javax.naming.InitialContext.createSubcontext(Unknown Source)
at JNDITest.main(JNDITest.java:22)



结果在执行绑定的时候就报错了,因为我们已经在上下文里绑定了这个name.



你可能感兴趣的:(JNDI)