javaazure-servicebus-queuesdead-letterbrokeredmessage

Reading deadlettered messages from service bus queue


I would like to know if it's possible to read deadlettered messages from an azure service bus queue in JAVA.

I found the following example https://code.msdn.microsoft.com/windowsazure/Brokered-Messaging-Dead-22536dd8/sourcecode?fileId=123792&pathId=497121593 However, I haven't been able to translate the code to JAVA.

I also found https://github.com/Azure/azure-storage-java/tree/master/microsoft-azure-storage/src/com/microsoft/azure/storage But there does not seem to be anything about deadlettering in there at all.

I also found several blogs (i'm not allowed to put more links so I don't know if I should anyway without proper tags). But they all do not describe how to read deadlettered messages in JAVA.

Much thanks in advance


Solution

  • I know it's an old thread but for next lost soul looking for a solution...

    I've been digging in the .NET SDK source and found that it's actually just a simple HTTP call to "/$DeadLetterQueue", i.e.:

    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"
    

    So using the Java SDK all you need to read messages from a Dead Letter Queue is:

    service.receiveQueueMessage(queueName + "/$DeadLetterQueue", opts);
    

    Here's a very basic but concrete example (destructive read):

    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;
            }
        }
    }