0

My application uses JNDI to access both local EJBs, and to access remote services via RMI. Currently this is handled in the following way:

public MyLocalEJB getMyLocalEJB(){
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"));
    env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
    env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"));

    InitialContext ctx = new InitialContext(environment);
    return (MyLocalEJB)ctx.lookup("MyEar/MyBean/local");
}

public RemoteServiceInterface getRemoveService(){
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.evermind.server.ApplicationClientInitialContextFactory"));
    env.put(Context.PROVIDER_URL, "ormi://server:port/APPNAME");
    env.put(Context.SECURITY_PRINCIPAL, "user");
    env.put(Context.SECURITY_CREDENTIALS, "secret");

    InitialContext ctx = new InitialContext(environment);
    return (RemoteServiceInterface)ctx.lookup("ejb/service/RemoteService");
}

This seems to give some issues though since Context.PROVIDER_URL overrides java.naming.provider.url which causes timing problems since change the property back and forth. For example, Hibernate Sessionfactory throws warning that the provider url is wrong, when it gets the RMI provider url instead of the local, and JBoss throws multiple exceptions on shutdown about jndi bindings.

I read that you can specify multiple provider URLs, so I tried the following in both methods:

env.put(Context.PROVIDER_URL, "jnp://localhost:1099,ormi://server:port/APPNAME");

This removes the errors, but it still feels bad since some other parameters change constantly depending on what method was called last (the username/pass for example should not be used when accessing local ejbs.).

I assume there is something wrong at the core of this implementation - how is it supposed to be properly done?

Rasmus Franke
  • 4,434
  • 8
  • 45
  • 62

1 Answers1

0

I don't think this is the cause of your issues. Both InitialContexts have separate environments, they both do different things. Do you have java.naming.provider.url set somewhere else as well? The information seems to be incomplete.

You need to close those contexts, in finally blocks.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • But the initialcontexts shares environment, if I create a new InitialContext with a provider url, it will set the global environment to this value until i create a new one to override it. This causes problems for usages where provider url is not specified and default it assumed. I do not modify the environment variable java.naming.provider.url myself. It is however used in an LDAP call in another class, but I dont think that has relevance here. – Rasmus Franke May 06 '13 at 07:37