1. Place the jndi.properties file on the classpath.
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory # use the following property to configure the default connector java.naming.provider.url = vm://localhost # use the following property to specify the JNDI name the connection factory # should appear as. connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry # register some queues in JNDI using the form # queue.[jndiName] = [physicalName] queue.MyQueue = example.MyQueue # register some topics in JNDI using the form # topic.[jndiName] = [physicalName] topic.MyTopic = example.MyTopic
2. Sample code
// create a new intial context, which loads from jndi.properties file javax.naming.Context ctx = new javax.naming.InitialContext(); // lookup the connection factory javax.jms.ConnectionFactory factory = (javax.jms.ConnectionFactory) ctx.lookup("connectionFactory"); // create a new Connection for messaging javax.jms.Connection conn = factory.createConnection(); // lookup an existing queue javax.jms.Destination destination = (javax.jms.Destination) ctx.lookup("MyQueue"); // create a new Session for the client javax.jms.Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); // create a new consumer to receive messages javax.jms.MessageConsumer consumer = session.createConsumer(destination);