- 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 | |
---|---|
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. |
- 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 (/)
-
Starts with
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 | |
---|---|
The use of the ENC naming convention is not required, but it is strongly encouraged! |
- Supports both local (optimized) and remote (over RMI) access to named objects
-
Provides a JVM-private app-shared
java:
context in addition to app-privatejava: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
- In your browser, navigate to http://localhost/jmx-console/
-
Under
jboss
context, click onservice=JNDIView
-
Invoke the
list(true)
operation -
Verify that in the
java:
namespace you can see JBoss services such asDefaultDS
,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.