在创建一个非常简单的JMS测试程序时出现了如下问题,请高手指教!
初学Java,请指教
1.J2EE 版本: 1.4
2.J2EE Default server has started
3.在J2EE admin console中创建了
JNDI Name: jms/MyQueueConnectionFactory
Type: javax.jms.QueueConnectionFactory
Status: enabled
JNDI Name: jms/MyTestQueue
Type: javax.jms.Queue
Status: enabled
然后保存
3.编写下面的类
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.*;
public class TestQueue {
protected static String sendUsage =
"TestQueue send queue-name [strings...]";
protected static String recvUsage =
"TestQueue recv queue-name";
// Change this variable's value to change the connection factory name
protected static String qfactoryName =
"jms/MyQueueConnectionFactory";
protected static void Usage(String message) {
if (message == null) {
message = sendUsage + "\n\t" + recvUsage;
}
System.err.println("Usage: " + message);
System.err.print("Read Enterprise Java Tech Tips, March 2003 ");
System.err.println("for instructions");
System.exit(1);
}
// This is the main program
public static void main(String[] args) {
if (args.length == 0) {
Usage(null);
}
String command = args[0];
try {
if (command.equalsIgnoreCase("send")) {
send(args);
} else if (command.equalsIgnoreCase("recv")) {
recv(args);
} else {
Usage(null);
}
} catch (NamingException nex) {
System.err.println("Naming exception : " + nex.getMessage());
nex.printStackTrace(System.err);
} catch (JMSException jex) {
System.err.println("JMS exception : " + jex.getMessage());
jex.printStackTrace(System.err);
System.err.println("Be sure j2ee server is running");
}
}
// Send a JMS message to a queue.
// Arguments:
// args[0] is the word "send"
// args[1] is the queue name
// args[2..end] are the strings to send as TextMessages
protected static void send(String[] args)
throws JMSException, NamingException {
String queueName;
if (args.length <= 1) {
Usage("sendUsage");
}
queueName = args[1];
QueueConnection qc = null;
try {
// Get JNDI context
InitialContext ctx = new InitialContext();
// Get connection factory
QueueConnectionFactory qcf =
(QueueConnectionFactory)ctx.lookup(qfactoryName);
// Get a connection to the queue
qc = qcf.createQueueConnection();
// Get a session from the connection
QueueSession qs =
qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Get a queue
Queue q = (Queue)ctx.lookup(queueName);
// Use the session to create a QueueSender and a TextMessage.
QueueSender qsnd = qs.createSender(q);
TextMessage tm = qs.createTextMessage();
// For each argument (after the first), send the argument
// string as a text message.
for (int i = 2; i < args.length; i++) {
System.out.println("Sent: '" + args[i] + "'");
tm.setText(args[i]);
qsnd.send(tm);
}
} finally {
if (qc != null) {
qc.close();
}
}
}
// Receive and print JMS messages from a queue
// Arguments:
// args[0] is the word "recv"
// args[1] is the queue name
protected static void recv(String[] args)
throws JMSException, NamingException {
String queueName;
if (args.length <= 1) {
Usage(recvUsage);
}
queueName = args[1];
QueueConnection qc = null;
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory qcf =
(QueueConnectionFactory)ctx.lookup(qfactoryName);
qc = qcf.createQueueConnection();
QueueSession qs =
qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue)ctx.lookup(queueName);
QueueReceiver qrcv = qs.createReceiver(q);
qc.start();
Message m = qrcv.receive(10000);
while (m != null) {
if (m instanceof TextMessage) {
TextMessage tm = (TextMessage)m;
System.out.println("Received text: '" +
tm.getText() + "'");
} else {
System.out.println("Received a " +
m.getClass().getName());
}
m = qrcv.receive(100);
}
} finally {
if (qc != null) {
qc.close();
}
}
}
}
编译ok!
4.执行如下命令
java TestQueue send jms/queue/MyTestQueue a b c d
出现如下错误
Naming exception : Need to specify class name in environment or system property,
or as an applet parameter, or in an application resource file: java.naming.fac
tory.initial
请问这是为什么?
我对JMS是这么理解的:
1.消息生产者 发消息到消息队列
2.消息队列对接收的消息持久化
3.消息消费者进程 从消息队列提取消息
是不是j2EE的admin console创建的connection Factory和Queue有问题?
//java TestQueue recv jms/queue/MyTestQueue
问题点数:50、回复次数:1Top
1 楼zhutouzip(醒了的鸟)回复于 2005-04-02 14:56:20 得分 50
写一个jndi.properties文件,加入下面的内容:
java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.provider.url=localhost:1099
java.naming.factory.url.pkgs=com.sun.enterprise.naming
=后面的内容根据不同的服务器适当改变,具体的参考这些服务气的文档!Top




