有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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) 个答案

  1. # 1 楼答案

    我假设您使用的是MessageListner,所以一旦出现消息,就会调用OnMessage()方法。在这种方法中,你可以保留一个计数器,一旦这个计数器达到允许的最大值,你就可以调用连接。stop()停止使用队列中的消息

    您可以随时通过调用连接重新启动。重新开始