Using JNDI to retrieve administered objects in a JMS or Jakarta Messaging application

To retrieve administered objects from a Java Naming and Directory Interface (JNDI) namespace, a JMS or Jakarta Messaging 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.

[JMS 2.0]For JMS 2.0, the administrator can use the IBM® MQ JMS administration tool, JMSAmin, or IBM 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.

[MQ 9.3.0 Jun 2022][MQ 9.3.0 Jun 2022][Jakarta Messaging 3.0]For Jakarta Messaging 3.0, you cannot administer JNDI using IBM MQ Explorer. JNDI administration is supported by the Jakarta Messaging 3.0 variant of JMSAdmin, which is JMS30Admin.

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:

[MQ 9.3.0 Jun 2022][MQ 9.3.0 Jun 2022][Jakarta Messaging 3.0]

import jakarta.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);
[JMS 2.0]

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.