How to synchronize placing an object in JNDI across processes?

Posted by indra on Stack Overflow See other posts from Stack Overflow or by indra
Published on 2010-05-03T16:59:18Z Indexed on 2010/05/03 17:08 UTC
Read the original article Hit count: 392

Filed under:
|

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:

private static String JNDI_NAME = "java:comp/env/test/something";

public class TestJNDI { private static String JNDI_NAME = "java:comp/env/test/something";

public static void main(String[] args) throws NamingException {

Hashtable env = new Hashtable(); 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(); }

}

© Stack Overflow or respective owner

Related posts about jndi

Related posts about syncronised