从队列中读取消息并将其显示给用户?

时间:2011-05-24 19:08:56

标签: java jms

我想首先知道队列中已存在多少条消息。以下类浏览器将返回Queue中存在的消息数。现在,我希望用户输入要从队列中读取的消息数,并仅向客户端显示该消息数。我不想读取队列中的所有消息,只读取用户想要读取的那些消息。请检查代码并回复应该做的事情。

public class Browser
{
    public static void main(String[] args) throws Exception
    {
    |   // get the initial context
    |   InitialContext ctx = new InitialContext();
    |                                                                      
    |   // lookup the queue object
    |   Queue queue = (Queue) ctx.lookup("queue/queue0");
    |                                                                      
    |   // lookup the queue connection factory
    |   QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
    |       lookup("queue/connectionFactory");
    |                                                                      
    |   // create a queue connection
    |   QueueConnection queueConn = connFactory.createQueueConnection();
    |                                                                      
    |   // create a queue session
    |   QueueSession queueSession = queueConn.createQueueSession(false,
    |       Session.AUTO_ACKNOWLEDGE);
    |                                                                      
    |   // create a queue browser
    |   QueueBrowser queueBrowser = queueSession.createBrowser(queue);
    |                                                                      
    |   // start the connection
    |   queueConn.start();
    |                                                                      
    |   // browse the messages
    |   Enumeration e = queueBrowser.getEnumeration();
    |   int numMsgs = 0;
    |                                                                      
    |   // count number of messages
    |   while (e.hasMoreElements()) {
    |   |   Message message = (Message) e.nextElement();
    |   |   numMsgs++;
    |   }
    |                                                                      
    |   System.out.println(queue + " has " + numMsgs + " messages");
    |                                                                      
    |   // close the queue connection
    |   queueConn.close();
    }
}


To read the number of messages as per user's requirements....
String NUMBER = request.getParameter("number"); 
.......
.......
.......
connection.start();
            for (int s = 0; s <= Integer.parseInt(NUMBER); s++){
             while (true){
                Message m = qReceiver.receive();
                if (m != null){
                    if (m instanceof BytesMessage){
                        BytesMessage bytesMessage = (BytesMessage)m;
                        PrintStream buffer = null;
                        for ( int i = 0; i < (int)bytesMessage.getBodyLength(); i++) {
                            buffer.append((char) bytesMessage.readByte());
                            }
                            String msg = buffer.toString().trim();

                            System.out.println("Reading Message: " + msg);
                       }else if (m instanceof TextMessage){
                        TextMessage textMessage = (TextMessage)m;
                        System.out.println("Reading message: " + textMessage.getText());
                    }else {
                        break;
                    }

1 个答案:

答案 0 :(得分:0)

我假设您正在使用MessageListner,因此一旦消息出现就会调用OnMessage()方法。在此方法中,您可以保留计数器,一旦此计数器达到允许的最大值,您可以调用connection.stop()来停止使用队列中的消息。

您可以通过再次调用connection.start()来重新启动。