Twisted
Twisted

Reputation: 3432

Can You Post a New Message Directly to the Dead Letter Queue in Azure Service Bus?

I have recently written some code which processes dead letter queue messages from azure service bus into a report.

I'm trying to test that code by uploading lots and lots of messages to a deadletter queue for it to process.

I've put together a c# linqpad / console app that reads a folder of text files and tries to send each one as a message to my testqueue/$deadletters using the Azure.Messaging.ServiceBus client library which looks like this:

var files = Directory.GetFiles(messagePath,filePattern);
var client = new ServiceBusClient(connectionString);
var dlqSender = client.CreateSender($"{queueName}/$deadletterqueue");

foreach (var file in files)
{
    var msgBody = File.ReadAllText(file);
    var message = new ServiceBusMessage(msgBody);
    await dlqSender.SendMessageAsync(message);
}

Unfortunately, this just freezes on the dlqSender.SendMessageAsync line for the 1st message and never resumes. Theres No error message and nothing goes into the DLQ or queue.

I appreciate it might be an odd usage scenario but I thought the DLQ behaved like any other queue. It would be good to get some confirmation that this is supported.

Upvotes: 2

Views: 2684

Answers (2)

Yash Gupta
Yash Gupta

Reputation: 2457

There are a couple of ways using which you can manually cause the messages to be published into Dead Letter Queues in Azure Service Bus.

  • You can use DeadLetterAsync() method, which sends the message to Dead Letter Queue.
  • You can define a very less TimeToLive value (zero, if you can) for the queue which is subscribing to those messages, which will cause the messages to straightaway go to Dead Letter Queue once the TTL expires. (Note: The EnableDeadLetteringOnMessageExpiration property must be set to true for this to happen)

One thing to note here is that, you might not immediately see the messages into Dead Letter Queue once you publish them, because the dead-lettered messages are handled by the default asynchronous Garbage Collection mechanism of Microsoft.Azure.ServiceBus package, so you need to wait sometime for the GC to do it's job & then you should be able to see your messages in Dead Letter Queue.

Upvotes: 1

Sean Feldman
Sean Feldman

Reputation: 25994

You can’t send a brand new message to the dead-letter queue directly. Dead-letter queue’s purpose is to store messages that cannot be processed from the parent queue/subscription. Therefore a message needs to be found in the parent queue first to make it into the dead-letter queue.

Upvotes: 0

Related Questions