3 Replies Latest reply on Jan 4, 2013 5:16 AM by Bernd Koecke

    No reuse of naming contexts with scoped EJB client context

    Bernd Koecke Novice

      I'm calling EJBs deployed on a JBossAS 7.2.0.Alpha1-Snapshot and I use the description from https://community.jboss.org/message/764451#764451 to generate the InitialContext from a Hashtable. But the naming context is never reused. My environment is:


      • Sun/Oracle Java 64-Bit Linux
      • JBossAS 7.2.0.Alpha1 from git
      • jboss-ejb-client 1.0.15.Final


      I create the InitialContext only once, but for each lookup through the InitialContext a new EJBNamingContext is created, which opens his own connections to all server nodes. The InitialContext only reuses a naming context from the naming provider when it was created from a factory configured by the Context.INITIAL_CONTEXT_FACTORY value from the JNDI environment. But the ejb client library uses the lookup URL scheme "ejb:". This causes a call to ejbURLContextFactory.getObjectInstance for each lookup. This context is only used for the resulting bean proxy.


      I would like to reuse the context for more than one bean proxy. So I build a small helper class for generating the context which gets the JNDI environment and use one entry as a key to store it

      in a map and reuse it when a context for the same cluster is needed. I expected to save resources (sockets, memory for the registry etc.), but because of the above it doesn't work.


      My question is, do I use it in the wrong way? Or do I misunderstand the meaning of reusing a naming context?


      I found two options to come around this:


      I can do my first lookup with the string "ejb:". This returns a subcontext which is a EjbNamingContext instance. When I store this in my helper classes map, I can reuse the context as expected.

      But the drawback is, that I don't have access to the EjbClientContext instance to register a listener for a context close event. And I have to remove "ejb:" from all lookup names.


      The other one is to use the ejbURLContextFactory.getObjectInstance directly. It is the only public available method. But then I loose the JDKs environment handling for the jndi.properties file.

      I don't use it at the moment. However when I need it, I could do


      {code}Context tmpCtx = new InitialContext(env);

      Hashtable<?,?> myEnv = tmpCtx.getEnvironment();{code}


      to get it and merge it with my own properties.


      Both options look a little bit hacky. I think I must be overlooking something, it would be nice when you can tell me what .