Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually publish messages to dead-letter queue?

Why would someone want to do that? I have to unit-test exception handling mechanism in our application.

I presumed that dead letter queue is literally azure service bus queue, where I could publish messages using QueueClient

string dlQ = @"sb://**.servicebus.windows.net/**/Subscriptions/DefaultSubscription/$DeadLetterQueue";
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
NamespaceManager _namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

QueueDescription qd = _namespaceManager.GetQueue(dataPromotionDLQ);
var queueClient = QueueClient.CreateFromConnectionString(connectionString, "DefaultSubscription/$DeadLetterQueue");
BrokeredMessage brokeredMessage = new BrokeredMessage("Message to PublishToDLQ");
try
{
    queueClient.Send(brokeredMessage);
}
catch (Exception)
{

}

But I get MessagingEntityNotFoundException. What could be wrong?

like image 839
Abhijeet Avatar asked Jan 23 '15 11:01

Abhijeet


2 Answers

You would never want to publish directly to a dead letter queue. It's where poisoned messages that can't be processed are placed.

There are two ways of placing messages onto the dead letter queue. The service bus itself dead-letters messages that have exceeded the maximum number of delivery attempts. You can also explicitly dead-letter a message that you have received using the DeadLetter() method.

like image 141
Ben Morris Avatar answered Oct 23 '22 15:10

Ben Morris


Create your messages with a very short TTL via the BrokeredMessage.TimeToLive property.

The Subscription must have EnableDeadLetteringOnMessageExpiration set to true.

like image 31
Mikee Avatar answered Oct 23 '22 16:10

Mikee