Jboss Admin Tutorial: JNDI Administration

8. JNDI Administration

8.1. Java Naming and Directory Interface

  • Core infrastructure (glue) for locating objects or services within an application server
  • Also allows external clients to locate services
  • Important for clustering: hides actual location
  • Divided into API and SPI

    • Applications code against the API
    • Application servers provide the SPI
  • SPIs for accessing remote resources, such as LDAP, DNS, NIS, file systems, RMI registry
[Note]Note

Even though JNDI is defined in Java SE, it has a fundamental role in Java EE. It provides the isolation for Java EE component code from the environment in which the code is deployed - i.e. easily customize the environment without modifying the source.

8.2. JNDI in Java EE

  • JNDI is to Java EE what DNS is to Internet apps
  • JNDI maps high-level names to resources like mail sessions, database connection pools, EJBs, and plain environmental properties
  • JNDI organizes its namespace using Environmental Naming Context (ENC) naming convention:

    • Starts with java:comp/env
    • Private to each application
    • Contexts are delimited with a forward-slash (/)

JNDI ENC naming convention:

  • java:comp/env/var - Environmental variables
  • java:comp/env/url - URLs
  • java:comp/env/mail - JavaMail sessions
  • java:comp/env/jms - JMS connection factories and destinations
  • java:comp/env/ejb - EJB home interfaces
  • java:comp/env/jdbc - JDBC DataSources
[Important]Important

The use of the ENC naming convention is not required, but it is strongly encouraged!

8.3. JNDI on JBoss

  • Supports both local (optimized) and remote (over RMI) access to named objects
  • Provides a JVM-private app-shared java: context in addition to app-private java:comp
  • Everything outside java: is public and externally visible
  • Exposes JNDI operations over JMX invoke operations - allows access over HTTP/S
  • Supports viewing JNDI Tree remotely
  • Supports clustering through HA-JNDI

Configured in ${jboss.server.config.url}/jboss-service.xml:

<mbean code="org.jboss.naming.NamingService"
       name="jboss:service=Naming"
       xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">
  ...
<mbean code="org.jboss.naming.JNDIView"
       name="jboss:service=JNDIView"
       xmbean-dd="resource:xmdesc/JNDIView-xmbean.xml">
</mbean>

Client configuration ${jboss.server.config.url}/jndi.properties:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Remote clients would need to add:

java.naming.provider.url=jnp://jbosshost.domain.com:1099

8.4. Lab: JNDI View

  • In your browser, navigate to http://localhost/jmx-console/
  • Under jboss context, click on service=JNDIView
  • Invoke the list(true) operation
  • Verify that in the java: namespace you can see JBoss services such as DefaultDS, DefaultJMSProvider, etc.
  • Deploy jndiview.war and compare results

We will cover JMX Console soon.

Note that jndiview.war updates the JNDI tree on every request, recording global visit counter. If you are interested, take a look at its source code.