2013-07-31 92 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()