我不能让JMS示例与Eclipse一起工作 [英] I can't get JMS example work with Eclipse
本文介绍了我不能让JMS示例与Eclipse一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Eclipse,当我尝试构建j2ee教程中包含的JMS示例时,它会返回一个NullPointerException:
I'm using Eclipse and when I try to build the JMS example included in j2ee tutorial, it return a NullPointerException at this:
connection = connectionFactory.createConnection();
以下是该示例的代码:
Here's the code of that example:
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.annotation.Resource;
public class Producer {
@Resource(lookup = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(lookup = "jms/Queue")
private static Queue queue;
@Resource(lookup = "jms/Topic")
private static Topic topic;
public static void main(String[] args) {
final int NUM_MSGS;
Connection connection = null;
if ((args.length < 1) || (args.length > 2)) {
System.err.println(
"Program takes one or two arguments: "
+ "<dest_type> [<number-of-messages>]");
System.exit(1);
}
String destType = args[0];
System.out.println("Destination type is " + destType);
if (!(destType.equals("queue") || destType.equals("topic"))) {
System.err.println("Argument must be \"queue\" or " + "\"topic\"");
System.exit(1);
}
if (args.length == 2) {
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}
Destination dest = null;
try {
if (destType.equals("queue")) {
dest = (Destination) queue;
} else {
dest = (Destination) topic;
}
} catch (Exception e) {
System.err.println("Error setting destination: " + e.toString());
e.printStackTrace();
System.exit(1);
}
try {
connection = connectionFactory.createConnection();
Session session = connection.createSession(
false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(dest);
TextMessage message = session.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText(
"This is message " + (i + 1) + " from producer");
System.out.println("Sending message: " + message.getText());
producer.send(message);
}
producer.send(session.createMessage());
} catch (JMSException e) {
System.err.println("Exception occurred: " + e.toString());
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
}</number-of-messages></dest_type>
如果我使用jndi,它运行正常。这个例子在Netbeans中有效。任何人都可以帮助我使用Eclipse吗?
if I use jndi, it works fine. This example worked in Netbeans. Can anyone help me to get it work with Eclipse?
推荐答案
看来你还没有设置connectionFactory
,这就是你尝试使用它时得到NullPointerException的原因。这不是直截了当的,而是开始这里 [ ^ 并且从那里开始。
彼得
投票给答案,如果他们回答你的问题,则标记为已接受。
It appears that you have not set upconnectionFactory
, which is why you get the NullPointerException when you try to use it. It's not straightforward, but start here[^] and go from there.
Peter
Vote for answers, and mark as accepted if they answer your question.
看来你还没有设置connectionFactory,这就是你尝试使用它时得到NullPointerException的原因。这不是直截了当的,而是从这里开始[^]并从那里开始。
It appears that you have not set up connectionFactory, which is why you get the NullPointerException when you try to use it. It's not straightforward, but start here[^] and go from there.
这篇关于我不能让JMS示例与Eclipse一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文