从服务总线队列读取死信消息

Pat*_*tro 1 java dead-letter azure-servicebus-queues brokeredmessage

我想知道是否可以从 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中读取死信消息。

预先非常感谢

Rai*_*een 6

我知道这是一个旧线程,但对于下一个迷失的灵魂寻找解决方案......

我一直在挖掘 .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"
Run Code Online (Sandbox Code Playgroud)

因此,使用 Java SDK 从死信队列读取消息所需要做的就是:

service.receiveQueueMessage(queueName + "/$DeadLetterQueue", opts);
Run Code Online (Sandbox Code Playgroud)

这是一个非常基本但具体的示例(破坏性阅读):

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)