Java @消息驱动不';我不能在JBoss AS 5中工作
下面给出的代码抛出一个Java @消息驱动不';我不能在JBoss AS 5中工作,java,ejb-3.0,jboss5.x,Java,Ejb 3.0,Jboss5.x,下面给出的代码抛出一个javax.naming.NameNotFoundException。我认为JBossAS5可能存在某种问题 package web; import java.util.Properties; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.
javax.naming.NameNotFoundException
。我认为JBossAS5可能存在某种问题
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) {
}
}
我应该亲自在服务器上创建它吗?我认为它是由此通知自动创建的??不是真的?缺少
destinationName
,它指示MDB将侦听消息的主题/队列
@MessageDriven(mappedName = "jms/txt", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destinationName", propertyValue = "jms/txt")
}
)
还要验证是否在管理控制台的服务器上创建了特定队列,查找是否失败。name notfoundexception表示您试图在JNDI中查找的名称不存在。因此,要么根本没有定义队列,要么使用了错误的名称 能否显示定义队列的xml文件 此外,正如Nayan所指出的,
destination
属性丢失。这是必需的。此外,此处使用的mappedName
注释属性完全错误,应该省略。此外,由于MDB使用默认的容器管理事务,acknowledgeMode
将被忽略,因此不需要指定
代码应该如下所示:
@MessageDriven(
activationConfig={
@ActivationConfigProperty(propertyName=“destinationType”,propertyValue=“javax.jms.Queue”),
@ActivationConfigProperty(propertyName=“destination”,propertyValue=“/queue/yourQueue”)
}
)
公共类FirstMDB实现MessageListener{
消息(消息消息消息)上的公共无效{
}
}
对于您的客户机,通常您还需要从远程JNDI查找ConnectionFactory
,不要忘记关闭从它获得的连接。对于JBoss AS 5.x和6.x,这个工厂的JNDI名称只是/ConnectionFactory
作为发送JMS消息的惯用示例:
ConnectionFactory=getFactoryFromJNDI();
连接=空;
试一试{
试一试{
connection=factory.createConnection();
会话会话=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Destination=getDestinationFromJNDI();
MessageProducer sender=session.createProducer(目的地);
Message Message=session.createTextMessage(“Hello World”);
发送者。发送(消息);
}
最后{
if(连接!=null){
connection.close();
}
}
}
捕获(JME){
// ...
}
其中
getFactoryFromJNDI()
和getDestinationFromJNDI()
只需包装JNDI查找代码。>我应该亲自在服务器上创建它吗?我认为它是由此通知自动创建的??不是真的?-您是否试图通过定义侦听JMS队列的bean来询问是否自动创建JMS队列?如果是这样,答案是否定的。您必须自己定义实际队列。