需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中指定

发布于 2024-11-29 01:45:43 字数 2715 浏览 2 评论 0原文

我是 JMS 的新手,我正在研究以下示例

public class SendRecvClient
{
    static CountDown done = new CountDown(1);
    QueueConnection conn;
    QueueSession session;
    Queue que;

public static class ExListener 
    implements MessageListener
{
    public void onMessage(Message msg)
    {
        done.release();
        TextMessage tm = (TextMessage) msg;
        try {
            System.out.println("onMessage, recv text=" + tm.getText());
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }
}

public void setupPTP()
    throws JMSException, 
           NamingException
{
    InitialContext iniCtx = new InitialContext();
    Object tmp = iniCtx.lookup("ConnectionFactory");
    QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
    conn = qcf.createQueueConnection();
    que = (Queue) iniCtx.lookup("queue/testQueue");
    session = conn.createQueueSession(false,
                                      QueueSession.AUTO_ACKNOWLEDGE);
    conn.start();
}

public void sendRecvAsync(String text)
    throws JMSException,
           NamingException
{
    System.out.println("Begin sendRecvAsync");
    // Setup the PTP connection, session
    setupPTP();

    // Set the async listener
    QueueReceiver recv = session.createReceiver(que);
    recv.setMessageListener(new ExListener());

    // Send a text msg
    QueueSender send = session.createSender(que);
    TextMessage tm = session.createTextMessage(text);
    send.send(tm);
    System.out.println("sendRecvAsync, sent text=" + tm.getText());
    send.close();
    System.out.println("End sendRecvAsync");
}

public void stop()
    throws JMSException
{
    conn.stop(); 
    session.close();
    conn.close();
}

public static void main(String args[])
    throws Exception
{
    SendRecvClient client = new SendRecvClient();
    client.sendRecvAsync("A text msg");
    client.done.acquire();
    client.stop();
    System.exit(0);
}

}

我在 JBoss 中运行了它,它给出了以下异常

Begin sendRecvAsync 线程“main”中的异常 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者作为小程序参数,或者在应用程序资源文件中:java.naming.factory.initial 在 javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) 在 javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 在 javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) 在 javax.naming.InitialContext.lookup(InitialContext.java:392) 在 se.cambio.jms.SendRecvClient.setupPTP(SendRecvClient.java:53) 在 se.cambio.jms.SendRecvClient.sendRecvAsync(SendRecvClient.java:68) 在 se.cambio.jms.SendRecvClient.main(SendRecvClient.java:95)

我认为这是 JNDI 名称的错误,但我找不到要在 JBOSS 中编辑哪个 xml 文件来解决此问题。请有人帮助我。

I'm new to JMS and I'm studying the following example

public class SendRecvClient
{
    static CountDown done = new CountDown(1);
    QueueConnection conn;
    QueueSession session;
    Queue que;

public static class ExListener 
    implements MessageListener
{
    public void onMessage(Message msg)
    {
        done.release();
        TextMessage tm = (TextMessage) msg;
        try {
            System.out.println("onMessage, recv text=" + tm.getText());
        } catch(Throwable t) {
            t.printStackTrace();
        }
    }
}

public void setupPTP()
    throws JMSException, 
           NamingException
{
    InitialContext iniCtx = new InitialContext();
    Object tmp = iniCtx.lookup("ConnectionFactory");
    QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
    conn = qcf.createQueueConnection();
    que = (Queue) iniCtx.lookup("queue/testQueue");
    session = conn.createQueueSession(false,
                                      QueueSession.AUTO_ACKNOWLEDGE);
    conn.start();
}

public void sendRecvAsync(String text)
    throws JMSException,
           NamingException
{
    System.out.println("Begin sendRecvAsync");
    // Setup the PTP connection, session
    setupPTP();

    // Set the async listener
    QueueReceiver recv = session.createReceiver(que);
    recv.setMessageListener(new ExListener());

    // Send a text msg
    QueueSender send = session.createSender(que);
    TextMessage tm = session.createTextMessage(text);
    send.send(tm);
    System.out.println("sendRecvAsync, sent text=" + tm.getText());
    send.close();
    System.out.println("End sendRecvAsync");
}

public void stop()
    throws JMSException
{
    conn.stop(); 
    session.close();
    conn.close();
}

public static void main(String args[])
    throws Exception
{
    SendRecvClient client = new SendRecvClient();
    client.sendRecvAsync("A text msg");
    client.done.acquire();
    client.stop();
    System.exit(0);
}

}

I ran this in JBoss and it gave the following exception

Begin sendRecvAsync
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(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at se.cambio.jms.SendRecvClient.setupPTP(SendRecvClient.java:53)
at se.cambio.jms.SendRecvClient.sendRecvAsync(SendRecvClient.java:68)
at se.cambio.jms.SendRecvClient.main(SendRecvClient.java:95)

I think this is an error with JNDI name, but I couldn't find which xml file to edit in JBOSS to over come this problem. Please some one help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文