Step 1: Add resource entry to CATALINA_HOME/conf/server.xml under GlobalNamingResources..
<Resource
name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker"
useEmbeddedBroker="false"
/>
<Resource
name="jms/Data"
auth="Container"
type="org.apache.activemq.command.ActiveMQTopic"
description="Data Topic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="queue.data"
/>
Step 2: Add resource link entry to CATALINA_HOME/conf/context.xml (For God knows What reason, An entry the context.xml in META-INF of my webapp did not work)
<ResourceLink global="jms/ConnectionFactory" name="jms/ConnectionFactory" type="javax.jms.ConnectionFactory"/>
<ResourceLink global="jms/Data" name="jms/Data" type="javax.jms.Topic"/>
Step 3: Add resource ref entry in web.xml
<resource-ref>
<res-ref-name>jms/ConnectionFactory</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<res-ref-name>jms/Data</res-ref-name>
<res-type>javax.jms.Topic</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Step 4: Put the following jars in CATALINA_HOME/lib from ActiveMQ_HOME/lib
activemq-core-5.1.0.jar
commons-logging-1.1.jar
geronimo-j2ee-management_1.0_spec-1.0.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-jta_1.0.1B_spec-1.0.1.jar
Step 5: Start the server, you should be able to use JNDI to connect to ActiveMQ.
Step 6: When you use IntialContext in your code, first get the context of "java:comp/env".
Context initCtx = new InitialContext();
jndiContext = (Context) initCtx.lookup("java:comp/env");
Then you can use:
connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/ConnectionFactory");
destination = (Destination)jndiContext.lookup("jms/Data");
Then, carry on with normal JMS flow.