Retrieving a connection factory from JNDI

 

To retrieve a ConnectionFactory object from a JNDI namespace, first set up an initial context as shown in the following code:

import javax.jms.*;
import javax.naming.*;
import javax.naming.directory.*;
 .
 .
 .
  java.util.Hashtable environment = new java.util.Hashtable();
  environment.put(Context.INITIAL_CONTEXT_FACTORY, icf);
  environment.put(Context.PROVIDER_URL, url);
  Context ctx = new InitialDirContext( environment );
In this code:

icf

Defines a factory class for the initial context

url

Defines a context specific URL

For more details about using a JNDI namespace, see Sun's JNDI documentation.

Some combinations of the JNDI packages and LDAP service providers can result in an LDAP error 84. To resolve the problem, insert the following line before the call to InitialDirContext:

environment.put(Context.REFERRAL, "throw");

After an initial context is obtained, we can retrieve a ConnectionFactory object from the JNDI namespace by using the lookup() method. The following code retrieves a ConnectionFactory object named CF from an LDAP based namespace:

ConnectionFactory factory;
factory = (ConnectionFactory)ctx.lookup("cn=CF");


uj24990_