Using JNDI to retrieve administered objects in a JMS application

To retrieve administered objects from a Java Naming and Directory Interface (JNDI) namespace, a JMS application must create an initial context and then use the lookup() method to retrieve the objects.

Before an application can retrieve administered objects from a JNDI namespace, an administrator must first create the administered objects. The administrator can use the IBM® MQ JMS administration tool or MQ Explorer to create and maintain administered objects in a JNDI namespace. For more information, see Configuring connection factories and destinations in a JNDI namespace.

An application server, typically provides its own repository for administered objects and its own tools for creating and maintaining the objects.

To retrieve administered objects from a JNDI namespace, an application must first create an initial context, as shown in the following example:

import javax.jms.*;
import javax.naming.*;
import javax.naming.directory.*;
.
.
.
String url = "ldap://server.company.com/o=company_us,c=us";
String icf = "com.sun.jndi.ldap.LdapCtxFactory";
.
java.util.Hashtable environment = new java.util.Hashtable();
environment.put(Context.PROVIDER_URL, url);
environment.put(Context.INITIAL_CONTEXT_FACTORY, icf);
Context ctx = new InitialDirContext(environment);
In this code, the String variables url and icf have the following meanings:
url
The uniform resource locator (URL) of the directory service. The URL can have one of the following formats:
  • ldap://hostname/contextName , for a directory service based on an LDAP server
  • file:/directoryPath , for a directory service based on the local file system
icf
The class name of the initial context factory, which can be one of the following values:
  • com.sun.jndi.ldap.LdapCtxFactory, for a directory service based on an LDAP server
  • com.sun.jndi.fscontext.RefFSContextFactory, for a directory service based on the local file system
Note that some combinations of a JNDI package and a Lightweight Directory Access Protocol (LDAP) service provider can cause LDAP error 84 to occur. To resolve this problem, insert the following line of code before the call to InitialDirContext():

environment.put(Context.REFERRAL, "throw");
After an initial context is obtained, the application can retrieve administered objects from the JNDI namespace by using the lookup() method, as shown in the following example:

ConnectionFactory factory;
Queue queue;
Topic topic;
.
.
.
factory = (ConnectionFactory)ctx.lookup("cn=myCF");
queue = (Queue)ctx.lookup("cn=myQ");
topic = (Topic)ctx.lookup("cn=myT");
This code retrieves the following objects from an LDAP based namespace:
  • A ConnectionFactory object bound with the name myCF
  • A Queue object bound with the name myQ
  • A Topic object bound with the name myT

For more information about using JNDI, see the JNDI documentation provided by Oracle Corporation.