0
創建連接下方,然後創建一個會話,然後從隊列中獲取一定量的消息。何時以及如何在異步消息監聽器的情況下關閉連接?
public class Consumer {
public static void main(String[] args) throws JMSException {
Consumer consumer = new Consumer(); // creates a connection and session
Destination destination = consumer.getSession().createQueue("JOBS");
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
messageConsumer.setMessageListener(new Listener(job)); // asynchronous listener.
consumer.close(); // closes the connection
}
}
何時以及如何關閉連接?因爲當我這樣做時,只有一條消息被讀取! 如果我沒有像上面那樣關閉連接,所有的消息都會被讀取,所以沒問題,但連接保持打開狀態。