How can I use the same InitialContext for EJB and other JNDI lookups in EAP6
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6.x
Issue
- The new ejb-client concept is for EJB lookups only and that it can't be used for other lookups, like getting JMS-ConnectionFactory. But this is what our client did and need!
- Is it possible to use the same InitialContext to lookup EJB's, JMS and other JNDI stuff.
- Is it possible to use "remote://host:4447" URL together with the jboss-ejb-client.properties?
Resolution
There are two different approaches to lookup resources from JNDI.
remote-naming project
With this approach it will be possible to access the JNDI directory.
It is not possible to use it for EJB lookup direct and it is not recommended to do so by setting the jboss.naming.client.ejb.context property as it will not provide the full functionality of the ejb-client approach.
ejb-client
With this approach it is possible to use the full power of EJB functionality.
It is not designed to provide other access to JNDI, only EJB related funtionality is provided.
Root Cause
It is possible to have a combination of remote-naming and ejb-client with the same InitialContext.
For the remote-naming the following code is necessary:
final Properties env = new Properties();
// this are the remote-naming properies
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "remote://localhost:4447");
env.put(Context.SECURITY_PRINCIPAL, "user");
env.put(Context.SECURITY_CREDENTIALS, "password");
context = new InitialContext(env);
// do a JNDI lookup
connectionFactory = (ConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
To lookup an EJB the ejb-client needs to be initialized. This can be done with a property based or programatic setup in a standalone application
The related code to setup the ejb-client programatically:
Properties p = new Properties();
p.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
p.put("remote.connections", "one");
p.put("remote.connection.one.port", String.valueOf(port));
p.put("remote.connection.one.host", host);
p.put("remote.connection.one.username", "user");
p.put("remote.connection.one.password", "password");
EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(p);
ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc);
EJBClientContext.setSelector(selector);
The same as the jboss-ejb-client.properties file looks like this:
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.username=user
remote.connection.default.password=password
To use both shown configuration in the same InitialContext You need to add the property Context.URL_PKG_PREFIXES with the value "org.jboss.ejb.client.naming" to the first shown example of remote-naming.
In that case the Context will use the remote-naming path to access JNDI but the EJB requests are handled by the ejb-client with the full functionality.
This is the recommended and supported approach with EAP6.
Note: This is different to the setting with the property jboss.naming.client.ejb.context to true. In that case the remote-naming will use an ejb-client with a minimal functionality, see this Content from docs.jboss.org is not included.documentation
If both properties jboss.naming.client.ejb.context and Context.URL_PKG_PREFIXES are set in the same InitialContext it might end in unlikely behavior.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.