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 Connectorjava.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 fileJavax.naming.Context CTX =NewJavax.naming.InitialContext ();//Lookup the Connection factoryJavax.jms.TopicConnectionFactory factory = (javax.jms.TopicConnectionFactory) ctx.lookup ("ConnectionFactory");//Create a new topicconnection for pub/sub messagingJavax.jms.TopicConnection conn =factory.gettopicconnection ();//Lookup an existing topicJavax.jms.Topic mytopic = (javax.jms.Topic) ctx.lookup ("Mytopic");//Create a new topicsession for the clientJavax.jms.TopicSession session = Conn.createtopicsession (false, Topicsession.auto_acknowledge);//Create a new Subscriber to receive messagesJavax.jms.TopicSubscriber subscriber = Session.createsubscriber (mytopic);
ActiveMQ (5.10.0)-JNDI support