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.
1 public class Main { 2 3 4 5 public static void main(String[] args) throws Exception { 6 7 Properties props = new Properties(); 8 9 // ActiveMQ10 11 // props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");12 13 // props.put(Context.PROVIDER_URL, "tcp://localhost:61616");14 15 16 17 // Tibco18 19 props.put(Context.INITIAL_CONTEXT_FACTORY, "com.tibco.tibjms.naming.TibjmsInitialContextFactory");20 21 props.put(Context.PROVIDER_URL, "tibjmsnaming://localhost:7222");22 23 24 25 // create a new intial context26 27 javax.naming.Context ctx = new javax.naming.InitialContext(props);28 29 30 31 // lookup an existing topic32 33 javax.jms.Topic myTopic = (javax.jms.Topic) ctx.lookup("MyTopic");34 35 System.out.println(myTopic);36 37 38 39 // lookup an existing queue40 41 javax.jms.Queue myQueue = (javax.jms.Topic) ctx.lookup("MyQueue");42 43 System.out.println(myQueue);44 45 }46 47 }
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.