I can't setup my jndi.properties to access remote EJBs on Jboss 5
An alternative way to do this is to configure a org.jboss.naming.ExternalContext
MBean in your jboss-service.xml file:
<mbean code="org.jboss.naming.ExternalContext"
name="jboss.jndi:service=ExternalContext,jndiName=external/server2">
<attribute name="JndiName">external/server2</attribute>
<attribute name="Properties">
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://10.90.0.91:1099
<!-- other properties as needed -->
</attribute>
<attribute name="InitialContext"> javax.naming.IntialContext </attribute>
<attribute name="RemoteAccess">false</attribute>
</mbean>
Your java code to perform the lookup then becomes:
Context initialContext = new InitialContext();
return initialContext.lookup("external/server2/" + jndiName);
You can even navigate the remote JNDI tree using JNDIView in the local management console when you set this up.
More information can be found in org.jboss.naming.ExternalContext MBean.
Well, I found another solution.
I created a new file called jndi-remote.properties on the configuration directory from Jboss:
{jboss_home}/server/default/conf/jndi-remote.properties
And I access the file in Jboss config directory (System.getProperty("jboss.server.config.url")
) from Java:
String fileName = System.getProperty("jboss.server.config.url") + "/" + "jndi-remote.properties";
Properties properties = null;
try {
URL url = new URL(fileName);
if(new File(url.toURI()).exists()) {
properties = new Properties();
properties.load(url.openStream());
LOGGER.info("The file " + "jndi-remote.properties" + " was loaded from " + fileName);
}
} catch (MalformedURLException e) {
//throw
} catch (URISyntaxException e) {
//throw
} catch (IOException e) {
//throw
}
And initialize my InitialContext:
if (properties != null) {
ctx = new InitialContext(properties);
}
Works :).