我想知道是否可以从 JAVA 中的 azure 服务总线队列读取死信消息。
我找到了以下示例https://code.msdn.microsoft.com/windowsazure/Brokered-Messaging-Dead-22536dd8/sourcecode?fileId=123792&pathId=497121593 但是,我无法将代码翻译为 JAVA。
我还发现了https://github.com/Azure/azure-storage-java/tree/master/microsoft-azure-storage/src/com/microsoft/azure/storage 但那里似乎根本没有关于死信的任何内容。
我还发现了几个博客(我不允许添加更多链接,所以我不知道我是否应该在没有适当标签的情况下)。但它们都没有描述如何在JAVA中读取死信消息。
提前非常感谢
最佳答案
我知道这是一个旧线程,但对于下一个迷失的灵魂寻找解决方案......
我一直在挖掘 .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;
}
}
}
关于java - 从服务总线队列读取死信消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288018/