下面給出的代碼將引發javax.naming.NameNotFoundException
。我想這可能是某種與JBoss的問題,因爲5@MessageDriven在JBoss AS 5中不起作用
package web;
import java.util.Properties;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import org.jboss.jms.server.connectionfactory.ConnectionFactory;
public class MyMDBClient {
public static void main(String[] args) {
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession session = null;
InitialContext ctx;
try {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming");
props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
ctx = new InitialContext(props);
Queue queue = (Queue) ctx.lookup("jms/txt");
QueueConnectionFactory factory = (QueueConnectionFactory)new ConnectionFactory();
cnn = factory.createQueueConnection();
session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = session.createTextMessage("Hello World");
sender = session.createSender(queue);
sender.send(msg);
System.out.println("Message sent successfully to remote queue.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
和mdb:
package web;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
@MessageDriven(mappedName="jms/txt",
activationConfig = { @ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue"
) })
public class FirstMDB implements MessageListener {
public void onMessage(Message message) {
}
}
我應該創建一個服務器上的個人而言,我認爲它這個全自動產生的? nottation ??是不是真的?
>我應該在服務器上創建它嗎?我認爲它是由這個nottation自動創建的?是不是真的? - 您是否試圖通過定義一個監聽它的bean來詢問是否自動創建JMS隊列?如果是這樣,答案是否定的。你必須自己定義實際的隊列。 –