How does JNDI lookup work in this JMS example?

I find it difficult to understand the JNDI part of the following JMS example.

public static void main(String[] args) { try { // Gets the JNDI context Context jndiContext = new InitialContext(); // Looks up the administered objects ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory"); Destination queue = (Destination) jndiContext.lookup("jms/javaee7/Queue"); // Sends a text message to the queue try (JMSContext context = connectionFactory.createContext()) { context.createProducer().send(queue, "Text message sent at " + new Date()); } } catch (NamingException e) { e.printStackTrace(); } } 

In the book where I gave this example, the setting was not mentioned to make this JNDI search possible. For example, in

 ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory"); 

should there be some kind of server running so jndiContext can get the ConnectionFactory object? In general, what setting is needed to find the JNDI above to work?

Thank you very much.

+6
source share
1 answer

In general, JNDI is a service that provides a set of objects that an application will use. This service is typically provided by an application server or web server or a dedicated LDAP server. If the tutorial you are trying to complete explains the JMS tutorial in the context of a web application, then most likely there are some settings on the application server (for example, Glassfish, JBoss) or a web server (for example, Tomcat). The way you access JNDI is also vendor-specific. This usually includes a configuration file (properties file or XML file). Another alternative to using JMS is to use a specialized JMS provider, such as ActiveMQ. Thus, you do not need an application server. Your application may just be a standalone java application (i.e., not necessarily a web application). Access to objects provided by ActiveMQ through JNDI is explained here: https://activemq.apache.org/jndi-support.html . General JNDI Tutorial: http://docs.oracle.com/javase/tutorial/jndi/

+8
source

All Articles