我是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中运行了它,它给出了以下异常
开始发送sendRecvAsync 线程“main”中的异常javax.naming.NoInitialContextException:需要在环境或系统属性或applet参数或应用程序资源文件中指定类名: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文件来解决这个问题。请有人帮助我。