Sun Java System Message Queue 4.2 Developer's Guide for Java Clients

ProcedureTo Look Up a Destination 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 LDAP Server Object Stores in Sun Java System Message Queue 4.2 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 destination object in the administered object store and typecast it to the appropriate class:


    String  DEST_LOOKUP_NAME = "MyDest";
    Destination  MyDest = (Destination) ctx.lookup(DEST_LOOKUP_NAME);

    The lookup name you use, DEST_LOOKUP_NAME, must match the name used when the object was stored. Note that the actual destination object returned from the object store will always be either a (point-to-point) queue or a (publish/subscribe) topic, but that either can be assigned to a variable of the generic unified-domain class Destination.


    Note –

    For topic destinations, a symbolic lookup name that includes wildcard characters can be used as the lookup string. See Supported Topic Destination Names in Sun Java System Message Queue 4.2 Administration Guide.


    You can now proceed to send and receive messages using the destination, as described under Sending Messages and Receiving Messages.