我使用createQueueBrowser迭代队列的所有消息,它一旦读取就从队列中删除所有消息,我想迭代遍历所有消息而不删除队列。怎么做?
public class Receive {
ConnectionFactory connectionFactory = null;
Connection connection = null;
void receivemsg() throws JMSException {
connectionFactory = new JmsConnectionFactory("RootManageSharedAccessKey",
"Gkt0Egjp/5YTrqAf9z+f2+HoRungEOh4OMSRA69js6M=",
"amqps://amqpqpid.servicebus.windows.net?amqp.idleTimeout=-1");
/*
* connectionFactory = new
* JmsConnectionFactory("RootManageSharedAccessKey",
* "Gkt0Egjp/5YTrqAf9z+f2+HoRungEOh4OMSRA69js6M=",
* "amqps://amqpqpid.servicebus.windows.net?amqp.idleTimeout=-1");
*/
connection = connectionFactory.createConnection();
connection.start();
System.out.println("Receiving messages...");
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination replyQueue = session.createQueue("ackqueue");
//MessageConsumer consumer = session.createConsumer(replyQueue);
TextMessage message = session.createTextMessage();
Queue recieve = session.createQueue("requestor");
QueueBrowser browser = session.createBrowser(recieve);
Enumeration msgs = browser.getEnumeration();
if (!msgs.hasMoreElements()) {
System.out.println("No messages in queue");
} else {
while (msgs.hasMoreElements()) {
Message tempMsg = (Message) msgs.nextElement();
System.out.println("Message: " + tempMsg.getJMSCorrelationID());
if(tempMsg.getJMSCorrelationID().equalsIgnoreCase("helloworld"))
{
System.out.println("matched and acked");
break;
}
}
}
}
public static void main(String args[]) throws JMSException {
Receive receiver = new Receive();
receiver.receivemsg();
System.out.println("Received Messages");
}
}
答案 0 :(得分:0)
我查看了Apache Qpid的文档,没有任何方法或选项可以支持消费者在AMQP客户端内无损。 Qpid的唯一方法是为Qpid代理服务器端的qpid.ensure_nondestructive_consumers
选项强制设置true
,但它不适合您当前的方案Azure Service Bus Queue
。
在Azure Service Bus Queue上,有一个REST API Peek-Lock Message (Non-Destructive Read)
可以满足您的需求。因此,您可以参考服务总线队列官方教程的Receive messages from a queue
部分,使用Azure Java SDK for Service Bus在下面的代码中设置ReceiveMode.PEEK_LOCK
来执行此操作。
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
希望它有所帮助。