如何跨进程同步将对象放入 JNDI 中?
我正在尝试将一个对象放入 JNDI 中,以便只有一个程序能够将其放入 JNDI 中。有没有可以在J2EE环境下使用的全局锁? RMI 可以用于此目的吗?请提供任何参考链接。提前致谢。
另外,什么是NameAlreadyBoundException?我试图将它用作一种同步方法,即,只有一个程序将其放置在 JNDI 中,如果其他尝试绑定的程序应该得到该异常。 但是当我测试多重绑定时,我没有收到异常。第二次绑定已完成。查找给出了第二个对象绑定。这是我的代码:
public class TestJNDI {
private static String JNDI_NAME = "java:comp/env/test/something";
public static void main(String[] args) throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");
Context ctx = new InitialContext(env);
System.out.println("Initial Context created");
String obj1 = "obj1";
String obj2 = "obj2";
try{
ctx.bind(JNDI_NAME, obj1);
System.out.println("Bind Sucess");
}catch(NameAlreadyBoundException ne ){
// already bound
System.out.println("Name already bound");
}
ctx.close();
Context ctx2 = new InitialContext(env);
try{
// Second binding to the same name not giving the Exception??
ctx2.bind(JNDI_NAME, obj2);
System.out.println("Re Bind Sucess");
}catch(NameAlreadyBoundException ne ){
// already bound
System.out.println("Name already bound");
}
String lookedUp = (String) ctx2.lookup(JNDI_NAME);
System.out.println("LookedUp Object"+lookedUp);
ctx2.close();
}
}
I am trying to place an object in JNDI, so that only one of the progam should be able to place it in JNDI. is there any global lock that can be used in J2EE environment. Is RMI can be used for this purpose? please provide any reference links. Thanks in advance.
Also, what is NameAlreadyBoundexception? I am trying to use it as a method to synchronize, i.e, only one program places it in JNDI and if other trying to bind should get that exception.
But when i am testing the multiple binding I am not getting the Exception.And second binding is done. look up is giving the second object bound. here is my code:
public class TestJNDI {
private static String JNDI_NAME = "java:comp/env/test/something";
public static void main(String[] args) throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001");
Context ctx = new InitialContext(env);
System.out.println("Initial Context created");
String obj1 = "obj1";
String obj2 = "obj2";
try{
ctx.bind(JNDI_NAME, obj1);
System.out.println("Bind Sucess");
}catch(NameAlreadyBoundException ne ){
// already bound
System.out.println("Name already bound");
}
ctx.close();
Context ctx2 = new InitialContext(env);
try{
// Second binding to the same name not giving the Exception??
ctx2.bind(JNDI_NAME, obj2);
System.out.println("Re Bind Sucess");
}catch(NameAlreadyBoundException ne ){
// already bound
System.out.println("Name already bound");
}
String lookedUp = (String) ctx2.lookup(JNDI_NAME);
System.out.println("LookedUp Object"+lookedUp);
ctx2.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您关闭第一个内容
ctx1
时,您将释放绑定到它的所有对象,请参阅:Context所以你的第二个上下文与第一个上下文无关。
When you close the first content
ctx1
you release any objects bound to it see : ContextSo your second context has nothing to do with the first one.