ActiveMQ will work with any JNDI provider capable of storing Java objects.
However it is common to require a JNDI initial context to be able to run many JMS example programs, like Sun's JMS tutorial
So we provide a simple JNDI InitialContextFactory which can be used to lookup JMS connection factory objects as well as Destination objects.
For example if you place this jndi.properties file on your classpath, you can look inside the InitialContext and lookup ConnectionFactory objects and Destinations etc.
You can edit the jndi.properties file to configure the ActiveMQConnectionFactory's properties such as brokerURL and whether or not there should be an embedded broker etc. See how to embed a broker in a connection for more details.
This is a quick one page tutorial on how to setup and use JNDI to create a connection to ActiveMQ. The first thing is ActiveMQ does not provide a full JNDI server. This means JMS clients need to use properties files to create a jndi IntialContextFactory. If you need an example properties file, you can look the source distribution http://svn.apache.org/viewvc/activemq/trunk/activemq-unit-tests/src/test/resources/jndi.properties?view=markup. Before we proceed, here are the properties.
Make sure to add activemq-<version>.jar and spring-1.x.jar to your classpath. If the libraries are not in the classpath, you will get a "ClassNotFoundException" at runtime. If you get "ClassNotFoundException", try printing out the classpath and check it is present. You can also run activeMQ with "-verbose" option to verify the jar was loaded correctly.
Notice the name of the topic in the sample is "MyTopic". ActiveMQ will read the jndi.properties files and creates the topics and queues in a lazy fashion. The prefix topic and queue is stripped, so the jndi name begins after the prefix.
Once you have the jndi.properties edited and ready, it needs to be accessible to your application. The easiest way is to add jndi.properties to a jar file. When "new InitialContext()" is called, it will scan the resources and find the file. If you get "javax.naming.NamingException", it usually means the jndi.properties file is not accessible.
You can also try to create a new initial context using either an instance of properties file or a map. For example, the approach recommended by JMS specification will work just fine.
If ActiveMQ is embedded within an EJB container, you will need to look at the containers documentation for the correct jndi values.
For the easiest possible configuration with JNDI based programs, there are 2 dynamic contexts as follows
which allows you to lookup queues and topics using JNDI without any configuration.
e.g. if you use the following name to lookup into JNDI
you will get back an ActiveMQQueue of the name "FOO.BAR".
This can be very handy if you can easily reconfigure the JNDI name to use to lookup something in JNDI, but don't want to have to double configure a jndi.properties to matchup.
It is often useful to use an embedded broker in the same JVM as the JMS client. For this see How do I embed a Broker inside a Connection.
If you want to use an embedded broker with your JNDI provider you can just use the VM Transport to connect to the broker in your URL. e.g. to create a purely in JVM broker use this URI
If you want to customize the broker use something like this
More options are available in the VM Transport Reference
Once you have configured JNDI on the classpath you can run any normal JMS application such as the following example. Notice that the Java code just uses pure JMS APIs and is not in any way ActiveMQ specific