我知道这是一个旧线程,但对于下一个迷失的灵魂正在寻找解决方案......
我一直在挖掘 .NET SDK 源代码,发现它实际上只是对“/$DeadLetterQueue”的简单 HTTP 调用,即:
https://mynamespace.servicebus.windows.net/myqueuename/$DeadLetterQueue/messages/head
// Peek-Lock Message from DLQ
curl -X POST -H "authorization: insertSASHere" "https://mynamespace.servicebus.windows.net/myqueuename/%24DeadLetterQueue/messages/head"
因此,使用 Java SDK 从死信队列中读取消息所需要做的就是:
service.receiveQueueMessage(queueName + "/$DeadLetterQueue", opts);
这是一个非常基本但具体的示例(破坏性读取):
public static void main(String[] args) throws ServiceException {
String namespace = "namespace";
String sharedKeyName = "keyName";
String sharedSecretKey = "secretKey";
String queueName = "queueName";
// Azure Service Bus Service
Configuration config = ServiceBusConfiguration.configureWithSASAuthentication(namespace, sharedKeyName, sharedSecretKey, ".servicebus.windows.net");
ServiceBusContract service = ServiceBusService.create(config);
// Receive and Delete Messages from DLQ
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.RECEIVE_AND_DELETE);
while (true) {
// To get messages from the DLQ we just need the "$DeadLetterQueue" URI
ReceiveQueueMessageResult resultQM = service.receiveQueueMessage(queueName + "/$DeadLetterQueue", opts);
BrokeredMessage message = resultQM.getValue();
if (message != null && message.getMessageId() != null) {
System.out.println("MessageID: " + message.getMessageId());
} else {
System.out.println("No more messages.");
break;
}
}
}