How to send the current Azure service bus message to Dead Letter queue? in typescript

Japnam Singh 71 Reputation points
2023-07-12T20:48:42.69+00:00
import { AzureFunction, Context } from "@azure/functions";

import{ ServiceBusClient,ServiceBusReceiver } from "@azure/service-bus";


const serviceBusQueueTrigger: AzureFunction = async function(context: Context, mySbMsg: any): Promise<void> {
    context.log('ServiceBus queue trigger function processed message', mySbMsg);
    
    const sbClient = new ServiceBusClient("Endpoint=sb://testasb2xxx");

	// createReceiver() can also be used to create a receiver for a queue.
	const receiver = sbClient.createReceiver(queueName);

	// function to handle messages
	const receiver: ServiceBusReceiver = sbClient.createReceiver('order-mr');

	const messages = await receiver.receiveMessages(1);

  
	if (messages.length) {
	  console.log(
		">>>>> Deadletter the one message received from the main queue - ",
		messages[0].body
	  );
	  // Deadletter the message received
	  await receiver.deadLetterMessage(messages[0], {
		deadLetterReason: "Incorrect Recipe type",
		deadLetterErrorDescription: "Recipe type does not match preferences.",
	  });
	} 
	 else {
	   console.log(">>>> Error: No messages were received from the main queue.");
	 }
  
	await receiver.close();
  }	
export default serviceBusQueueTrigger;

So, when we send 1 message to the queue, we want to move the mySBMsg to dead letter however we are getting no messages in the receiver .
but if we have 2 messages in the queue, we will get messages under receiver.
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
{count} vote

Accepted answer
  1. MayankBargali-MSFT 70,941 Reputation points Moderator
    2023-07-17T05:24:50.8966667+00:00

    @Anonymous Thanks for reaching out.

    Message will be moved to dead letter queue only due to the reasons that are mentioned here. There is no way/method to move the message to the dead letter queue.

    HeaderSizeExceeded The size quota for this stream has been exceeded.
    TTLExpiredException The message expired and was dead lettered. See the Time to live section for details.
    Session ID is null. Session enabled entity doesn't allow a message whose session identifier is null.
    MaxTransferHopCountExceeded The maximum number of allowed hops when forwarding between queues has been exceeded. This value is set to 4.
    MaxDeliveryCountExceeded Message couldn't be consumed after maximum delivery attempts. See the Maximum delivery count section for details.

    In case if you found that you don't want to consume a particular message then you need to keep on calling the abandoned method on that particular message so the max delivery count is reach and then message will be moved to the dead letter queue.

    we are getting no messages in the receiver . but if we have 2 messages in the queue, we will get messages under receiver.

    If there are no message in the queue, then you will receive the empty response by receiver.receiveMessages and as I see you are trying to receive one message. Can you please confirm if you have validate that the message was send form your client? Can you use service bus explorer/or azure portal to validate the message count or peek the message to confirm. As this need to further troubleshoot as per the code receiver.receiveMessages(1) it should receive one message. There could be scenario where you running your application and there was no message received then it may wait for 30 seconds to again verify if there are any new messages received.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.