java 从队列中读取消息并将其显示给用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6115555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a message from a Queue and display it to the user?
提问by xyz
I want to know first how many messages are already existing in a Queue. The following class Browser will return the number of messages existing in Queue. Now, I want user to enter the number of messages to be read from a queue and display only that number of messages to the client. I do not want to read all the messages from a queue but only those number of messages which user wants to read. Please check the code and reply what should be done.
我想首先知道队列中已经存在多少条消息。以下类浏览器将返回队列中存在的消息数。现在,我希望用户输入要从队列中读取的消息数量,并仅向客户端显示该数量的消息。我不想从队列中读取所有消息,而只想读取用户想要读取的消息数量。请检查代码并回复应该做什么。
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;
}
回答by Ravi Bhatt
i assume that you are using a MessageListner, so OnMessage() method will be called once a message comes up. In this method you can keep a counter, once this counter reaches maximum allowed, you can call connection.stop() to stop consuming messages from the queue.
我假设您使用的是 MessageListner,因此一旦出现消息,就会调用 OnMessage() 方法。在这个方法中你可以保留一个计数器,一旦这个计数器达到允许的最大值,你可以调用 connection.stop() 来停止消耗队列中的消息。
You can always restart by calling connection.start() again.
您始终可以通过再次调用 connection.start() 重新启动。