Sun GlassFish Message Queue 4.4 Developer's Guide for Java Clients

Looking Up a Connection Factory With JNDI

Example 2–1 shows how to look up a connection factory object in the JNDI object store. The code example is explained in the procedure that follows.


Note –

If a Message Queue client is a J2EE component, JNDI resources are provided by the J2EE container. In such cases, JNDI lookup code may differ from that shown here; see your J2EE provider documentation for details.



Example 2–1 Looking Up a Connection Factory


//  Create the environment for constructing the initial JNDI 
//  naming context.
    
    Hashtable  env = new Hashtable();
    
    
//  Store the environment attributes that tell JNDI which initial context
//  factory to use  and where to find the provider.// 
    
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
                    "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL, "file:///C:/imq_admin_objects");
    
    
//  Create the initial context.
    
    Context  ctx = new InitialContext(env);
    
    
//  Look up the connection factory object in the JNDI object store.
    
    String  CF_LOOKUP_NAME = "MyConnectionFactory";
    ConnectionFactory  myFactory = (ConnectionFactory) ctx.lookup
                                          (CF_LOOKUP_NAME);
    
               

ProcedureTo Look Up a Connection Factory With JNDI

  1. Create the environment for constructing the initial JNDI naming context.

    How you create the initial context depends on whether you are using a file-system object store or a Lightweight Directory Access Protocol (LDAP) server for your Message Queue administered objects. The code shown here assumes a file-system store; for information about the corresponding LDAP object store attributes, see Using an LDAP User Repository in Sun GlassFish Message Queue 4.4 Administration Guide

    The constructor for the initial context accepts an environment parameter, a hash table whose entries specify the attributes for creating the context:


    Hashtable  env = new Hashtable();

    You can also set an environment by specifying system properties on the command line, rather than programmatically. For instructions, see the README file in the JMS example applications directory.

  2. Store the environment attributes that tell JNDI which initial context factory to use and where to find the JMS provider.

    The names of these attributes are defined as static constants in class Context:


    env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL, "file:///C:/imq_admin_objects");

    Note –

    The directory represented by C:/imq_admin_objects must already exist; if necessary, you must create the directory before referencing it in your code.


  3. Create the initial context.


    Context  ctx = new InitialContext(env);

    If you use system properties to set the environment, omit the environment parameter when creating the context:


    Context  ctx = new InitialContext();
  4. Look up the connection factory object in the administered object store and typecast it to the appropriate class:


    String  CF_LOOKUP_NAME = "MyConnectionFactory";
    ConnectionFactory
          myFactory = (ConnectionFactory) ctx.lookup(CF_LOOKUP_NAME);

    The lookup name you use, CF_LOOKUP_NAME, must match the name used when the object was stored.

    You can now proceed to use the connection factory to create connections to the message broker, as described under Using Connections.