Introduction
Recently our team was working on Veracity Quick Start sprint, when I was trying to migrate the JMS provider implementation from Tibco EMS to ActiveMQ. I found that there are notable differences between these two JMS implementations on their JNDI support, which will be illustrated below.
Code to demonstrate
Code below is a main function which use Java JNDI API to retrieve JMS queue and topic. We will try each implementation by put relevant INITIAL_CONTEXT_FACTORY and PROVIDER_URL into the props object.
public class Main { public static void main(String[] args) throws Exception { Properties props = new Properties(); // ActiveMQ // props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); // props.put(Context.PROVIDER_URL, "tcp://localhost:61616"); // Tibco props.put(Context.INITIAL_CONTEXT_FACTORY, "com.tibco.tibjms.naming.TibjmsInitialContextFactory"); props.put(Context.PROVIDER_URL, "tibjmsnaming://localhost:7222"); // create a new intial context javax.naming.Context ctx = new javax.naming.InitialContext(props); // lookup an existing topic javax.jms.Topic myTopic = (javax.jms.Topic) ctx.lookup("MyTopic"); System.out.println(myTopic); // lookup an existing queue javax.jms.Queue myQueue = (javax.jms.Topic) ctx.lookup("MyQueue"); System.out.println(myQueue); } }
Try Tibco EMS
When using Tibco EMS, in order to let the code run successfully, i.e. retrieved myTopic and myQueue objects:
1) There must be a Tibco JMS server running on localhost:7222.
2) A topic named MyTopic and a queue named MyQueue must already be defined.
No other configuration files needed.
Try ActiveMQ
When you try ActiveMQ(uncomment line 5 and 6, and comment line 9 and 10), things are little different:
1) You don’t need to have MyTopic and MyQueue already exists on ActiveMQ server.
2) You even needn’t have a server running at localhost:61616.
3) Put a jndi.properties file on your class path, in which you predefined the wanted queues and topics:
Or use a little different syntax when invoke lookUp in your java code: use dynamicTopics/MyTopic instead of MyTopic, and dynamicQueues/MyQueue instead of MyQueue.
Conclusion
I checked the documentation of both products, also relevant sections in the book ActionMQ in action, and figure out the differences about their JNDI capability:
1) Tibco EMS implements a server-side JNDI provider, you actually need to communicate to server to get your defined queues and topics.
2) ActiveMQ implements a client-side JNDI provider, no communication with server is needed.
When you get queue or topic object by a name passing to lookUp(), it doesn’t means they already exist on server, nor they will be created at that moment. ActiveMQ queues and topics are dynamically created when you actually use them, for example, when you actually send a message to a queue.
Suffice to say, the JNDI support is completely local and has nothing to do with server.