2011-07-27 42 views
0

下面給出的代碼將引發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 ??是不是真的?

+0

>我應該在服務器上創建它嗎?我認爲它是由這個nottation自動創建的?是不是真的? - 您是否試圖通過定義一個監聽它的bean來詢問是否自動創建JMS隊列?如果是這樣,答案是否定的。你必須自己定義實際的隊列。 –

回答

0

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") 
    } 
) 

而且驗證特定隊列是否在管理控制檯服務器上創建,查找正在失敗。

0

nameNotFoundException表示您試圖在JNDI中查找的名稱不存在。所以,要麼你沒有定義一個隊列,要麼你使用了錯誤的名字。

您可以顯示定義隊列的xml文件嗎?

此外,正如Nayan也指出,destination屬性丟失。這是必需的。另外,您在這裏使用mappedName註釋屬性是完全錯誤的,應該省略。此外,由於MDB正在使用默認的容器管理事務,因此acknowledgeMode被忽略,因此不需要指定。

的代碼應該是這樣的:

@MessageDriven( 
    activationConfig = {   
     @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), 
     @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/yourQueue") 
    } 
) 
public class FirstMDB implements MessageListener { 

    public void onMessage(Message message) {  

    }  
} 

至於你的客戶,通常還需要查找遠程JNDI的ConnectionFactory不要忘了關閉從中獲得連接。對於JBoss AS 5.x和6.x,這家工廠的JNDI名稱只是/ConnectionFactory

作爲發送JMS消息的慣用例如:

ConnectionFactory factory = getFactoryFromJNDI(); 
Connection connection = null; 
try { 
    try { 
     connection = factory.createConnection(); 

     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);    
     Destination destination = getDestinationFromJNDI(); 

     MessageProducer sender = session.createProducer(destination);  
     Message message = session.createTextMessage("Hello World"); 

     sender.send(message);   
    } 
    finally { 
     if (connection != null) { 
      connection.close(); 
     } 
    } 
} 
catch (JMSException e) { 
    // ... 
} 

getFactoryFromJNDI()getDestinationFromJNDI()簡單地包裹JNDI查找代碼。