Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

How to use a JNDI Provider

Qpid will work with any JNDI provider capable of storing Java objects. We have a task to add our own initial context factory, but until that's available ....

First you must select a JNDI provider to use. If you aren't already using an application server (i.e. Tomcat ?) which provides JNDI support you could consider using either:

  • Apache's Directory which provides an LDAP JNDI implementation
  • Click : Download JNDI 1.2.1 & More button
  • Download: File System Service Provider, 1.2 Beta 3
  • and then add the two jars in the lib dir to your class path.

There are two steps to using JNDI objects.

  • Bind : Which stores a reference to a JMS Object in the provider.
  • Lookup : Which tries to retrieve the reference and create the JMS Object.

There are two objects that would normally be stored in JNDI.

  • A ConnectionFactory
  • A Destination (Queue or Topic)

Binding

Then you need to setup the values that the JNDI provider will used to bind your references, something like this:

Code Block
titleSetup JNDI
borderStylesolid
Hashtable env = new Hashtable(11);
  env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
  env.put(Context.PROVIDER_URL,LOCAL_FILE_PATH_FOR_STORING_BINDS_PATH_MUST_EXIST);

These values are then used to create a context to bind your references.

Code Block
titlePerform Binding of ConnectionFactory
borderStylesolid
try
{
    Context ctx = new InitialContext(env);

    // Create the object to be bound in this case a ConnectionFactory
    ConnectionFactory factory = null;

    try
    {
        factory = new AMQConnectionFactory(CONNECTION_URL);
        try
        {
            ctx.bind(binding, factory);
        }
        catch (NamingException e)
        {
            //Handle problems with binding. Such as the binding already exists.
        }
    }
    catch (URLSyntaxException amqe)
    {
        //Handle any exception with creating ConnnectionFactory
    }
}
catch (NamingException e)
{
    //Handle problem creating the Context.
}

To bind a queue instead simply create a AMQQueue object and use that in the binding call.

Code Block
titleBind a AMQQueue
borderStylesolid
AMQQueue  queue = new AMQQueue(QUEUE_URL);
ctx.bind(binding, queue);

Lookup

You can then get a queue connection factory from the JNDI context.

Code Block
titlePerform Binding of ConnectionFactory
borderStylesolid
ConnectionFactory factory;
try
{
    factory= (ConnectionFactory)ctx.lookup(binding);
}
catch (NamingException e)
{
    //Handle problems with lookup. Such as binding does not exist.
}

Note that you need not cast the bound object back to an AMQConnectionFactory so all your current JMS apps that use JNDI can start using Qpid straight away.

How to create a TopicConnectionFactory and QueueConnectionFactory

AMQConnectionFactory implements TopicConnectionFactory and QueueConnectionFactory as well as the ConnectionFactory.