JBoss.org Community Documentation

2.2.1.1. Accessing HA-JNDI Resources from EJBs and WARs -- Environment Naming Context

If your HA-JNDI client is an EJB or servlet, the least intrusive way to configure the lookup of resources is to bind the resources to the environment naming context of the bean or webapp performing the lookup. The binding can then be configured to use HA-JNDI instead of a local mapping. Following is an example of doing this for a JMS connection factory and queue (the most common use case for this kind of thing.

Within the bean definition in the ejb-jar.xml or in the war's web.xml you will need to define two resource-ref mappings, one for the connection factory and one for the destination.

<resource-ref>
	<res-ref-name>jms/ConnectionFactory</res-ref-name>
	<res-type>javax.jms.QueueConnectionFactory</res-type>
	<res-auth>Container</res-auth>
</resource-ref>
	
<resource-ref>
	<res-ref-name>jms/Queue</res-ref-name>
	<res-type>javax.jms.Queue</res-type>
	<res-auth>Container</res-auth>
</resource-ref>

Using these examples the bean performing the lookup can obtain the connection factory by looking up 'java:comp/env/jms/ConnectionFactory' and can obtain the queue by looking up 'java:comp/env/jms/Queue'.

Within the JBoss-specific deployment descriptor (jboss.xml for EJBs, jboss-web.xml for a WAR) these references need to mapped to a URL that makes use of HA-JNDI.

<resource-ref>
	<res-ref-name>jms/ConnectionFactory</res-ref-name>
	<jndi-name>jnp://localhost:1100/ConnectionFactory</jndi-name>
</resource-ref>
	
<resource-ref>
	<res-ref-name>jms/Queue</res-ref-name>
	<jndi-name>jnp://localhost:1100/queue/A</jndi-name>
 </resource-ref>

The URL should be the URL to the HA-JNDI server running on the same node as the bean; if the bean is available the local HA-JNDI server should also be available. The lookup will then automatically query all of the nodes in the cluster to identify which node has the JMS resources available.