`

JNDI

    博客分类:
  • Java
 
阅读更多
JNDI 与 静态变量 有什么不同: 命名目录接口,功能和代码中的静态变量有点相同,不过静态变量只在当前程序中使用,而 JDNI 可以有好几个程序来使用。

(
    JNDI走的是网络,因此共享资源是多进程,多机器之间.而静态变量只是本进程共享.
    JNDI主要作用是为分布式开发作服务的,当你需要网络上其它主机给你提供服务时候,可以将该主机的服务注册成JNDI,之后直接从JNDI处获取服务.
)

How to use:

1.创建初始上下文环境
Properties p = new Properties();
//JNDI Server Type 
p.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
//JNDI Server URL
p.put(javax.naming.Context.PROVIDER_URL, "t3://localhost:7001");
//Set Security (Optional)
p.put(javax.naming.Context.SECURITY_PRINCIPAL, "User_Name");
p.put(javax.naming.Context.SECURITY_CREDENTIALS, "User_Password");
//JNDI Context Initial
javax.naming.Context ctx = null;
try {
	ctx = new InitialContext(p);
} catch (NamingException e) {
	e.printStackTrace();
}


2.JNDI操作
2.1 JNDI对象绑定
try {
	String nameString = "Hello World";
	ctx.bind("name", nameString);
} catch (NamingException e) {
	e.printStackTrace();
}

2.2 JNDI对象的重新绑定
try {
	String nameString = "Hello World Rebind";
	ctx.bind("name", nameString);
} catch (NamingException e) {
	e.printStackTrace();
}

2.3 JNDI对象的删除
try {
	ctx.unbind("name");
} catch (NamingException e) {
	e.printStackTrace();
}

2.4 JNDI对象的查找
try {
	String nameStr = (String)ctx.lookup("name");
} catch (NamingException e) {
	e.printStackTrace();
}

2.5 注意重复绑定的问题
try {
	Object obj = ctx.lookup("name");
	if(obj instanceof String)
		ctx.unbind("name");
	String newString = "New World";
	ctx.bind("name", newString);
} catch (NamingException e) {
	e.printStackTrace();
}


How to implement:
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics