Sunday, October 25, 2015

There are two JNDI binding for EJB

This is so ridiculous, now only I got to realize there are two types of EJB JNDI context available when connecting the client to the server. Assuming I have the following JNDI binding ready:
   java:global/ejb1/AuthenticationImpl!org.huahsin.AuthenticationRemote
   java:app/ejb1/AuthenticationImpl!org.huahsin.AuthenticationRemote
   java:module/AuthenticationImpl!org.huahsin.AuthenticationRemote
   java:jboss/exported/ejb1/AuthenticationImpl!org.huahsin.AuthenticationRemote
   java:global/ejb1/AuthenticationImpl
   java:app/ejb1/AuthenticationImpl
   java:module/AuthenticationImpl
The first is org.jboss.naming.remote.client.InitialContextFactory, this will require additional library, jboss-client.jar to be loaded in the classpath. And also this is the most hassle free and easy to setup. The following code shows how this could be done:
   Properties props = new Properties();
   props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
   props.put(Context.PROVIDER_URL, "remote://127.0.0.1:4447");

   InitialContext context = new InitialContext(props);

   AuthenticationRemote bean = (AuthenticationRemote) context.lookup("ejb1/AuthenticationImpl!org.huahsin.AuthenticationRemote");
The second is org.jboss.ejb.client.naming, it consists of two parts. The first is the setup in the code, as shown in the following:
   Hashtable props = new Hashtable();
   props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
  
   InitialContext context = new InitialContext(props);

   String appName = "";
   String moduleName = "ejb1";
   String distinctName = "";
   String beanName = AuthenticationImpl.class.getSimpleName();
   String interfaceName = AuthenticationRemote.class.getName();
   String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + interfaceName;
  
   AuthenticationRemote bean = (AuthenticationRemote) context.lookup(name);
The second part would be the configuration file, jboss-ejb-client.properties, to be placed in the classpath. Missing this would not be able to establish connection to the server. The content of the configuration is as follows:
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
 
remote.connections=default
 
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
Phew! Finally, I got the things clear.

No comments: