Menu Search

Appendix E. How to bind Qpid destinations and connection factories into Tomcat JNDI

Qpid client destinations and connection factories can be registered in external JNDI containers, for example, Tomcat JNDI implementation.

org.apache.qpid.jndi.ObjectFactory implements javax.naming.spi.ObjectFactory allowing it to create instances of AMQConnectionFactory, PooledConnectionFactory, AMQConnection, AMQQueue and AMQTopic in external JNDI container from javax.naming.References.

Additionally, AMQConnectionFactory, PooledConnectionFactory and AMQDestination (parent of AMQQueue and AMQTopic) implement javax.naming.Referenceable allowing creation of javax.naming.Reference objects for binding in external JNDI implementations.

org.apache.qpid.jndi.ObjectFactory allows the creation of:

Note

For AMQQueue and AMQTopic prefix BURL: need to be specified for Binding URL. Otherwise, client will try to parse content using Address format.

An example below demonstrates how to create JNDI resources in the Tomcat container using Resource declarations in context.xml (A Tomcat specific web application configuration file usually added into war under /META-INF/context.xml).

Example E.1. An example of Tomcat context.xml declaring Qpid JNDI resources

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE xml>
<Context>

  <Resource name="jms/connectionFactory" auth="Container"
            type="org.apache.qpid.client.AMQConnectionFactory"
            factory="org.apache.qpid.jndi.ObjectFactory"
            connectionURL="amqp://guest:guest@clientid/?brokerlist='localhost:5672'"/>

  <Resource name="jms/pooledConnectionFactory" auth="Container"
            type="org.apache.qpid.client.PooledConnectionFactory"
            factory="org.apache.qpid.jndi.ObjectFactory"
            connectionURL="amqp://guest:guest@clientid/?brokerlist='localhost:5672'"
            maxPoolSize="20" connectionTimeout="60000"/>

  <Resource name="jms/queue" auth="Container"
            type="org.apache.qpid.client.AMQQueue"
            factory="org.apache.qpid.jndi.ObjectFactory"
            address="BURL:direct://amq.direct//myQueue?durable='true'"/>

  <Resource name="jms/topic" auth="Container"
            type="org.apache.qpid.client.AMQTopic"
            factory="org.apache.qpid.client.AMQConnectionFactory"
            address="BURL:topic://amq.topic//myTopic?routingkey='myTopic'"/>

</Context>

In the example above AMQConnectionFactory would be registered under JNDI name "jms/connectionFactory", PooledConnectionFactory would be registered under JNDI name "jms/pooledConnectionFactory", Queue "myQueue" would be registered under JNDI name "jms/queue" and JMS Topic destination "myTopic" would be registered under JNDI name "jms/topic". (All resources will be bound under "java:comp/env"). On declaration of PooledConnectionFactory optional maxPoolSize and connectionTimeout are set to 20 and 60000 milliseconds accordingly.

The client application can find the resources declared in Tomcat context.xml using the code below:

Example E.2. An example of JNDI lookup for Qpid resources registered in Tomcat JNDI

    Context context = new InitialContext();
    Context environmentContext = (Context)context.lookup("java:comp/env");
    ...
    ConnectionFactory connectionFactory = (ConnectionFactory) environmentContext.lookup("jms/connectionFactory");
    ...
    Queue queue = (Queue)environmentContext.lookup("jms/queue");
    ...
    Topic topic = (Topic)environmentContext.lookup("jms/topic");
    ...

Note

In order to support backward compatibility AMQConnectionFactory continues to implement javax.naming.spi.ObjectFactory and can be used to instantiate JNDI resources from javax.naming.References. However, its method getObjectInstance is marked as Deprecated and will be removed in future version of client. For backward compatibility, Qpid JNDI resources can be declared using fully qualified class names as addresses. That will became unsupported in future version as well. An example of Tomcat context.xml with declarations of JNDI resources using deprecated factory and addresses is provided below.

Example E.3. An example of Tomcat context.xml declaring Qpid JNDI resources using deprecated ObjectFactory and deprecated addresses

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE xml>
<Context>

  <Resource name="jms/queue" auth="Container"
            type="org.apache.qpid.client.AMQQueue"
            factory="org.apache.qpid.client.AMQConnectionFactory"
            org.apache.qpid.client.AMQQueue="direct://amq.direct//myDurableQueue?durable='true'"/>

  <Resource name="jms/topic" auth="Container"
            type="org.apache.qpid.client.AMQTopic"
            factory="org.apache.qpid.client.AMQConnectionFactory"
            org.apache.qpid.client.AMQTopic="topic://amq.topic//myTopic?routingkey='myTopic'"/>

  <Resource name="jms/connectionFactory" auth="Container"
            type="org.apache.qpid.client.AMQConnectionFactory"
            factory="org.apache.qpid.client.AMQConnectionFactory"
            org.apache.qpid.client.AMQConnectionFactory="amqp://guest:guest@clientid/?brokerlist='localhost:5672'"/>

</Context>