2013-07-31 95 views
0

我是jms的初學者,我只是試圖讓一個簡單的web應用程序運行,產生並消費消息。所以在我的歡迎頁面上,我有一個帶有文本框和提交按鈕的表單。在提交文本框中的文本被包裝在消息中並通過MessageProducer發送到服務器上的隊列。據我所知,這條消息應該保留在隊列中,直到我從消費者鏈接到該隊列的某個MessageConsumer調用receive方法。不幸的是在我的第二個jsp頁面上,我單擊「獲取消息」按鈕,收到的消息變爲空。我很確定他們正在被正確發送(至少沒有錯誤,否則)有沒有辦法我可以檢查,但?任何想法出現什麼問題?提前致謝。繼承人生產者/消費者代碼。無法接收jms隊列中的消息

public class Producer {   
    private static final String CONNECTION_FACTORY = "connectionFactory";  
    private static final String CONNECTION_QUEUE = "jms/myqueue";  
    private static final String CONNECTION_TOPIC = "jms/mytopic";  

    public Producer(String message) {   
     ConnectionFactory connectionFactory = null;  
     Connection connection = null;   
     //Get the JNDI Context   
     try {   
      Context jndiContext = new InitialContext();  

      //Create the Connection Factory and Connection  
      connectionFactory = (ConnectionFactory) jndiContext.lookup(Producer.CONNECTION_FACTORY);  
      connection = connectionFactory.createConnection(); 

      //Create the session   
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);     

      publishToQueue(jndiContext, session, message);  
      session.commit(); 
     } catch (NamingException e) {    
     } catch (JMSException e) {  

     }finally{  
      //close connection  
     } 
    }   

    private void publishToQueue(Context jndiContext, Session session, String message) throws NamingException, JMSException{  
     //Create Queue  
     Queue queue = (Queue) jndiContext.lookup(Producer.CONNECTION_QUEUE); 

     //Create Message Producer  
     MessageProducer producer = session.createProducer(queue);  

     //Send TextMessage   
     producer.setDeliveryMode(DeliveryMode.PERSISTENT); 
     TextMessage textMessage = session.createTextMessage();  
     textMessage.setText(message); 
     producer.send(textMessage); 
     producer.close(); 

} 
} 


public class Consumer {   
    private static final String CONNECTION_FACTORY = "connectionFactory";  
    private static final String CONNECTION_QUEUE = "jms/myqueue";  
    private static final String CONNECTION_TOPIC = "jms/mytopic";  

    private Message message; 

    public Consumer() {   
     ConnectionFactory connectionFactory = null;  
     Connection connection = null;   
     //Get the JNDI Context   
     try {   
      Context jndiContext = new InitialContext();  

     //Create the Connection Factory  
     connectionFactory = (ConnectionFactory) jndiContext.lookup(Consumer.CONNECTION_FACTORY);  
     connection = connectionFactory.createConnection(); 

     //Create the session   
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);     

     //Call methods to retrieve message  
     message = getFromQueue(jndiContext, session);  
     session.commit(); 
     if(message!=null){ 
      System.out.println("Message Received"); 
     } 
    } catch (NamingException e) {    
     // TODO Auto-generated catch block  
     e.printStackTrace();   
    } catch (JMSException e) {  
     // TODO Auto-generated catch block  
     e.printStackTrace();  

    }finally{  
     try {   
      if(connection != null){  
       connection.close();  
      }    
     } catch (JMSException e) { 
      e.printStackTrace();   
     }  
    } 
}   



private Message getFromQueue(Context jndiContext, Session session) throws NamingException, JMSException{  
    //Create new Queue 
    Queue queue = (Queue)jndiContext.lookup(Consumer.CONNECTION_QUEUE); 
    //Create Message Consumer  
    MessageConsumer consumer = session.createConsumer(queue); 
    return consumer.receive(10000);  
} 

public Message getMessage(){ 
    return message; 
} 
} 

回答

1

對不起,發現其他問題的答案.. MessageConsumer is not consuming messages .. 所有我需要的是getFromQueue方法

public Consumer() {   
    ConnectionFactory connectionFactory = null;  
    Connection connection = null;   
    //Get the JNDI Context   
    try {   
     Context jndiContext = new InitialContext();  

    //Create the Connection Factory  
    connectionFactory = (ConnectionFactory) jndiContext.lookup(Consumer.CONNECTION_FACTORY);  
    connection = connectionFactory.createConnection(); 

    //Create the session   
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);     

    //Call methods to retrieve message 
    --> connection.start(); <-- 
    message = getFromQueue(jndiContext, session);  
    session.commit(); 
    if(message!=null){ 
     System.out.println("Message Received"); 
    } 
之前調用connection.start()