简体   繁体   English

如何在 jms 中使用 JNDI

[英]How to use JNDI in jms

i am learning JMS tried a simple example below but it gives error我正在学习 JMS 在下面尝试了一个简单的例子,但它给出了错误

package pointToPoint;
import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Receiver
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext ctx = new InitialContext();

       // lookup the queue object
       Queue queue = (Queue) ctx.lookup("queue/queue0");

       // lookup the queue connection factory
       QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
           lookup("queue/connectionFactory");

       // create a queue connection
       QueueConnection queueConn = connFactory.createQueueConnection();

       // create a queue session
       QueueSession queueSession = queueConn.createQueueSession(false,
           Session.AUTO_ACKNOWLEDGE);

       // create a queue receiver
       QueueReceiver queueReceiver = queueSession.createReceiver(queue);

       // start the connection
       queueConn.start();

       // receive a message
       TextMessage message = (TextMessage) queueReceiver.receive();

       // print the message
       System.out.println("received: " + message.getText());

       // close the queue connection
       queueConn.close();
    }
}

error message:错误信息:

Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at pointToPoint.Receiver.main(Receiver.java:22)

can anyone please help me with this.任何人都可以帮我解决这个问题。

The answer is in the stack trace.答案在堆栈跟踪中。 In a stand-alone application, you should pass an environment (implementation, urls, etc) to the InitialContext.在独立应用程序中,您应该将环境(实现、url 等)传递给 InitialContext。 Inside a J2EE container like JBoss, your code will work.在 JBoss 等 J2EE 容器中,您的代码将起作用。

This should help: NoInitialContextException error这应该有帮助: NoInitialContextException 错误

What the responder above meant was that the tutorial would help you send messages to a receiver on the same application server.上面的响应者的意思是本教程将帮助您将消息发送到同一应用程序服务器上的接收者。 A lot more is needed for remote lookups远程查找需要更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM