开发者

How to synchronize placing an object in JNDI across processes?

开发者 https://www.devze.com 2022-12-29 04:28 出处:网络
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 pu

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 bo开发者_JAVA技巧und
     System.out.println("Name already bound");
      } 


    String lookedUp = (String) ctx2.lookup(JNDI_NAME);

    System.out.println("LookedUp Object"+lookedUp);
    ctx2.close();
  }


}


When you close the first content ctx1 you release any objects bound to it see : Context

So your second context has nothing to do with the first one.

0

精彩评论

暂无评论...
验证码 换一张
取 消