Java JNDI 查找在这个 JMS 示例中是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23037845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:12:35  来源:igfitidea点击:

How does the JNDI lookup work in this JMS example?

javajmsjndi

提问by JBT

I am having a hard time to understand the JNDI part of the following JMS example.

我很难理解以下 JMS 示例的 JNDI 部分。

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();
    }
}

The book where I got this example didn't mention the setup to make this JNDI lookup possible. For example, in

我得到这个例子的书没有提到使这个 JNDI 查找成为可能的设置。例如,在

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

should there be some kind of server running so that the jndiContextcan get a hold of a ConnectionFactoryobject? In general, what sort of setup is required for the JNDI lookup above to work?

是否应该运行某种服务器以便jndiContext可以获取ConnectionFactory对象?一般来说,上面的 JNDI 查找需要什么样的设置才能工作?

Thank you very much.

非常感谢。

采纳答案by suhe_arie

In general, JNDI is a service that provides a set of objects to be used by application. This service is usually provided by application server or web server or a dedicated LDAP server. If the tutorial you are trying to follow explains the JMS tutorial in the context of web application, then most likely there are some setups to be done in the application server (e.g. Glassfish, JBoss) or web server (e.g. Tomcat). The way to access JNDI also depends on the provider. Usually, this involves a configuration file (either properties file, or XML file). Another alternative to use JMS is to utilize a dedicated JMS provider such as ActiveMQ. This way, you don't need any application server. Your application can just be a standalone java application (i.e. not necessarily a web application). Accessing objects provided by ActiveMQ via JNDI is explained here: https://activemq.apache.org/jndi-support.html. General JNDI tutorial: http://docs.oracle.com/javase/tutorial/jndi/

一般来说,JNDI 是一种服务,它提供一组供应用程序使用的对象。此服务通常由应用程序服务器或 Web 服务器或专用 LDAP 服务器提供。如果您尝试遵循的教程在 Web 应用程序的上下文中解释了 JMS 教程,那么很可能需要在应用程序服务器(例如 Glassfish、JBoss)或 Web 服务器(例如 Tomcat)中完成一些设置。访问 JNDI 的方式也取决于提供者。通常,这涉及配置文件(属性文件或 XML 文件)。使用 JMS 的另一种替代方法是使用专用的 JMS 提供程序,例如 ActiveMQ。这样,您就不需要任何应用服务器。您的应用程序可以只是一个独立的 Java 应用程序(即不一定是 Web 应用程序)。此处解释了通过 JNDI 访问 ActiveMQ 提供的对象:https://activemq.apache.org/jndi-support.html。一般 JNDI 教程:http: //docs.oracle.com/javase/tutorial/jndi/