EJB3.0在发布时出现异常javax.naming.NameNotFoundException: myqueue not bound

时间:2023-01-19 21:59:20
  Service:
 
package com.test.ejb;
 
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.ejb.ActivationConfigProperty;
@MessageDriven(
 activationConfig ={
   @ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),
   @ActivationConfigProperty(propertyName="destination",propertyValue="queue/myqueue")
 }
)
 
public class MyQueueMDBBean implements MessageListener {
 
 public void onMessage(Message msg) {
  TextMessage textMessage =(TextMessage)msg;
  
  try {
   System.out.println("MyQueueMDBBean被调用了【"+textMessage.getText()+"】");
   
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 }
 
}
 
Client:
 
 
package com.test.ejb;
 
import javax.jms.JMSException;
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 javax.naming.NamingException;
 
public class MyQueueMDBBeanClient {
 
 /**
  * @param args
  */
 public static void main(String[] args) {
 
  try {
   //获取QueueConnectionFactory对象
   InitialContext context =new InitialContext();
   QueueConnectionFactory factory =(QueueConnectionFactory)context.lookup("ConnectionFactory");
   
   //创建QueueConnection
   QueueConnection connnection =factory.createQueueConnection();
   
   //创建QueueSession
   QueueSession session =connnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
   
   //获取Destination对象
   Queue queue =(Queue)context.lookup("queue/myqueue");
   
   //获取文本信息
   TextMessage msg =session.createTextMessage("世界你好啊!");
   
   //创建发送者
   QueueSender sender =session.createSender(queue);
   
 //关闭会话
   session.close();
   connnection.close();

   //发送消息
   sender.send(msg);
   
   System.out.println("消息已发送");
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }
 
}
 
发布启动Jboss时出现异常:
javax.naming.NameNotFoundException: myqueue not bound
 at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
 at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
 at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
 at org.jnp.server.NamingServer.lookup(NamingServer.java:443)
 at org.jnp.server.NamingServer.lookup(NamingServer.java:399)
 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:713)
 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:673)
 at javax.naming.InitialContext.lookup(InitialContext.java:392)
 at org.jboss.util.naming.Util.lookup(Util.java:222)
 at org.jboss.resource.adapter.jms.inflow.JmsActivation.setupDestination(JmsActivation.java:464)
 at org.jboss.resource.adapter.jms.inflow.JmsActivation.setup(JmsActivation.java:352)
 at org.jboss.resource.adapter.jms.inflow.JmsActivation$SetupActivation.run(JmsActivation.java:729)
 at org.jboss.resource.work.WorkWrapper.execute(WorkWrapper.java:204)
 at org.jboss.util.threadpool.BasicTaskWrapper.run(BasicTaskWrapper.java:260)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:619) 
 
 
  解决办法:
 
 我发现在JBoss根目录 \server\default\deploy下的mail-service.xml文件中加入如下:

<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.org.destination:server=Queue,name=myqueue" >
  <attribute name="JNDIName" >queue/myqueue</attribute>
  <depends optional-attribute-name = "DestinationManager" > jboss.mq:service=DestinationManager </depends>
</mbean>

重启JBoss问题即解决。