jboss6 消息配置

jboss6 消息配置

看了尚学堂的视频,发现queue消息老是提示javax.naming.NameNotFoundException:
发现原来是jboss版本的问题
  1. 需要在server\default\deploy\hornetq\hornetq-jms.xml配置jndi
<queue name="testQueue">  
    <entry name="/queue/queueA"/>   
</queue>  

2. 需要修改客户端发送方式
原来的方式:
 try {
		 InitialContext initCon = new InitialContext();
		
		 // 获得QueueConectionFactory 对象
		 QueueConnectionFactory factory = (QueueConnectionFactory)
		 initCon.lookup("ConnectionFactory");
		
		 // 创建QueueConnection
		 QueueConnection connection = factory.createQueueConnection();
		
		 //创建QueueSession
		 QueueSession session = connection.createQueueSession(true,
		 QueueSession.AUTO_ACKNOWLEDGE);
		
		 //获取Destination
		 Queue queue = (Queue) initCon.lookup("/queue/myqueue");
		
		 //创建文本消息
		 TextMessage msg = session.createTextMessage("HELLO!!!!!!!!!!!!");
		
		 //创建发送者
		 QueueSender send = session.createSender(queue);
		
		 //发送消息
		 send.send(msg);
		
		 session.close();
		 connection.close();
		 System.out.println("发送完成"); 
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

现在的方式:
		try {

			InitialContext init = new InitialContext();

			ConnectionFactory cf = (ConnectionFactory) init

			.lookup("ConnectionFactory");

			Connection con = cf.createConnection();

			Session session = con

			.createSession(false, Session.AUTO_ACKNOWLEDGE);

			Destination dest = (Destination) init.lookup("/queue/myqueue");

			MessageProducer prd = session.createProducer(dest);

			prd.send(session.createTextMessage("fudongnihao"));

			session.close();

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
 

你可能感兴趣的:(jboss)