我正在嘗試使用JMS執行一個簡單的發件人 - 接收器示例。我無法運行JMS示例
我已經寫在Eclipse中的代碼,但是當我做了我的查找收到此錯誤:
需要在環境或系統屬性指定的類名,或者作爲一個小程序參數,或應用程序資源文件:java.naming.factory.initial的
在這裏你可以找到的代碼:
接收機:
public class Receiver {
static Context ictx = null;
public static void main(String[] args) throws Exception {
ictx = new InitialContext();
Queue queue = (Queue) ictx.lookup("queue");
QueueConnectionFactory qcf = (QueueConnectionFactory) ictx.lookup("qcf");
ictx.close();
QueueConnection qc = qcf.createQueueConnection();
QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueReceiver qrec = qs.createReceiver(queue);
TextMessage msg;
qc.start();
int i;
for (i = 0; i < 10; i++) {
msg = (TextMessage) qrec.receive();
System.out.println("Msg received: " + msg.getText());
}
qc.close();
}
}
發件人:
public class Sender {
static Context ictx = null;
public static void main(String[] args) throws Exception {
ictx = new InitialContext();
Queue queue = (Queue) ictx.lookup("queue");
QueueConnectionFactory qcf = (QueueConnectionFactory) ictx.lookup("qcf");
ictx.close();
QueueConnection qc = qcf.createQueueConnection();
QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender qsend = qs.createSender(queue);
TextMessage msg = qs.createTextMessage();
for (int i = 0; i < 10; i++) {
msg.setText("Test number " + i);
qsend.send(msg);
}
qc.close();
}
}
哪裏出錯?我需要在像Glassfish這樣的服務器上運行它嗎?