JBoss JNDI Binding configuration
- Published: 28 July 2012
This tutorial shows how you can configure some JNDI bindings at application server startup, showing all possible application server variants (JBoss AS 4/5/6/7)
The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and objects via a name. Like all other Java APIs also, JNDI is independent of the underlying implementation, however it specifies a service provider interface (SPI) that allows directory service implementations to be plugged into the framework.
The Enterprise resources (such as Datasources and JMS destinations) are stored in the JNDI tree so that they can be consumed by applications which are deployed on the application server. Neverthless you can use JNDI to store attributes which will be used by the server/severs a bit like application properties. In this case the advantage of using JNDI instead of basic Properties is that JNDI provides a tree structure for bindings which you don't have in a simple Property file. (Please note that if you plan to store/cache data structures achieving fully control on them, consider using Infinispan framework).
Let's see how you can configure your application server to store some JNDI bindings in different AS versions:
JBoss AS 7
JBoss AS 7 ships wth a naming subsystem which contains the bindings element. In order to add some JNDI bindings to the application server just add some name/value attributes in it:
<subsystem xmlns="urn:jboss:domain:naming:1.1"> <bindings> <simple name="jndi/mykey" value="MyJndiValue"/> </bindings> </subsystem>
Naming alias was introduced in earlier JBoss AS release to create a link from a JNDI binding to another. You can think about it as a symbolic link in Unix Systems. This can be useful for example if you are migrating from one application server JNDI binding structure to another and the JNDI bindings are stored in your application code.
In order to achieve naming aliases you can use the name and lookup attribute of the lookup element:
<subsystem xmlns="urn:jboss:domain:naming:1.1"> <bindings> <lookup name="java:global/MyOldEJB" lookup="java:global/my-ear/my-ejb-module/ExampleEJB"/> </bindings> </subsystem>
JBoss AS 4-5-6
In earlier JBoss AS releases, the org.jboss.naming.JNDIBindingServiceMgr is a service which allows one to bind arbitrary values into JNDI.
<server> <mbean code="org.jboss.naming.JNDIBindingServiceMgr" name="pm-urls:service=JNDIBindingServiceMgr"> <attribute name="BindingsConfig" serialDataType="jbxb"> <jndi:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns:jndi="urn:jboss:jndi-binding-service:1.0" xs:schemaLocation="urn:jboss:jndi-binding-service:1.0 resource:jndi-binding-service_1_0.xsd"> <jndi:binding name="jndi/binding/port"> <jndi:value type="java.lang.Integer">443</jndi:value> </jndi:binding> <jndi:binding name="jndi/binding/protocol"> <jndi:value type="java.lang.String">https</jndi:value> </jndi:binding> </jndi:bindings> </attribute> <depends>jboss:service=Naming</depends> </mbean> </server>
The NamingAlias MBean is the utility service that allows you to create an alias in the form of a JNDI javax.naming.LinkRef from one JNDI name to another. To an alias you add a configuration of the NamingAlias MBean to the jboss-service.xml configuration file. The configurable attributes of the NamingAlias service are as follows:
FromName : The location where the LinkRef is bound under JNDI.
ToName : The to name of the alias. This is the target name to which the LinkRef refers. The name is a URL, or a name to be resolved relative to the InitialContext, or if the first character of the name is a dot (.), the name is relative to the context in which the link is bound.
The following example provides a mapping of the JNDI name QueueConnectionFactory to the name ConnectionFactory.
<mbean code="org.jboss.naming.NamingAlias" name="jboss.mq:service=NamingAlias,fromName=QueueConnectionFactory"> <attribute name="ToName">ConnectionFactory</attribute> <attribute name="FromName">QueueConnectionFactory</attribute> </mbean>