Before using JNDI to either store or retrieve objects, an "initial context"
must be set up, as shown in this fragment taken from the MQeJMSIVT_JNDI example
program:
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 InitialContext(environment );
where:
- icf
- defines a factory class for the JNDI context. This depends upon the JNDI
provider that you are using. The documentation supplied by the JNDI provider
should tell you what value to use for this. See also the examples below.
- url
- defines the location of the namespace. This will depend on the type of
namespace you are using. If you are using the file system, this will be a
file url that identifies a directory in your file system. If you are using
LDAP this will be a ldap url that identifies a LDAP server and location in
the directory tree of that server. The documentation supplied by the JNDI
provider should describe the correct format for the url.
For more details about JNDI usage, see Sun's JNDI documentation.
environment.put(Context.REFERRAL,"throw");
Once
an initial context is obtained, objects can be stored in and retrieved from
the namespace. To store an object, use the
bind() method:
ctx.bind(entryName, object);
where 'entryName' is
the name under which you want the object stored, and 'object' is the object
to be stored, for example to store a factory under the name "ivtQCF":
ctx.bind("ivtQCF", factory);
To
store an object in a JNDI namespace, the object must satisfy either the javax.naming.Referenceable
interface or the java.io.Serializable interface, depending on the JNDI provider
you use. The
MQeJNDIQueueConnectionFactory and
MQeJMSJNDIQueueclasses
implement both of these interfaces. To retrieve an object from the namespace,
use the
lookup() method:
object = ctx.lookup(entryName);
where
entryName is
the name under which you want the object stored , for example, to retrieve
a
QueueConnectionFactory stored under the name "ivtQCF":
QueueConnectionFactory factory;
factory = (QueueConnectionFactory)ctx.lookup("ivtQCF");