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.
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]](ng930cd.gif)
![[MQ 9.3.0 Jun 2022]](ng930.gif)
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]](ng930cd.gif)
![[MQ 9.3.0 Jun 2022]](ng930.gif)
![[Jakarta Messaging 3.0]](ngjm30.gif)
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]](ngjms20.gif)
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);
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 serverfile:/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 servercom.sun.jndi.fscontext.RefFSContextFactory, for a directory service based on the local file system
environment.put(Context.REFERRAL, "throw");
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.