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

标签 java azure-servicebus-queues dead-letter 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中读取死信消息。

提前非常感谢

最佳答案

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

我一直在挖掘 .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/

相关文章:

java - 无法使用 sudo 以其他用户身份执行命令

azure - 将消息安排到 Azure 队列的机制

amazon-web-services - 有什么方法可以在特定条件下从 AWS lambda 函数内部将事件消息显式发送到死信队列?

deployment - 发送给正在部署的 Actor 的消息会发生什么情况?

azure - 将过期的死信消息重新提交回队列

Java 正则表达式与预期不匹配

java - Swing中JTextField的透明边框

java - 使用 OpenCSV 解析包含 Unicode 字符的 CSV 文件

c# - 确定消息在 Azure 主题上出现的次数

azureservicebus - Azure SB 队列将消息发送到禁用的死信队列