0

I am using Azure Service Bus JS sdk to receive messages from a ASB topic. The receiver is using the default mode (i.e. the Peek) mode. And I call completeMessage() in my message handler. What I am seeing is, the message handler is being called multiple times with the same message. And the sender only sends the message once.

const myMessageHandler = async (messageReceived) => {
      this.receiver.completeMessage(messageReceived);
     // process messageReceived
     .....
} 
this.receiver.subscribe({
    processMessage: myMessageHandler,
    processError: myErrorHandler
});

Can you please tell me what can be the issue? I think ASB is delivery the message multiple times despite the receives already complete the message.

Sam
  • 5
  • 2
  • You can refer to [Service bus queue trigger with Azure function calling multiple times for same message](https://stackoverflow.com/a/44051584), [Azure Service Bus: Duplicate messages are processing in message queue](https://stackoverflow.com/a/54949533) and [ServiceBusTrigger triggers multiple times the same message](https://github.com/Azure/azure-functions-host/issues/2796). You can also open an issue on GitHub: [azure-sdk-for-js](https://github.com/Azure/azure-sdk-for-js/issues) – Ecstasy Oct 11 '21 at 06:02

1 Answers1

0

Generally, in Azure Service Bus a messages will be re-processed if no action was done to that message by the receiving party. And that action can be completing, deferring, dead-lettering.

But this should not the issue in your case as your are using the completeMessage() action to the message you are receiving. So, the problem you are facing could be due to an exception. As a general best practice, it’s a good idea to handle all exceptions in the message processing code so the lock duration is never reached as shown in the code snippet below.

    ...
    try { 
            //Process message code 
    } 
    catch (Exception ex) {
            //Abandon message
    }
    ...
    ...

Check what exception you are getting. One I can thing of is MessageLockLostException, it due to the default value of autoComplete. Note that the default value of AutoComplete is true. Then if we are going to use completeMessage() action then we should set autoComplete as false. Or else it can result to MessageLockLostException.

If you are still facing the problem check this Config options suggested by @Tomas. Also check the Best Practices for performance improvements using Service Bus Messaging document for more information.

SauravDas-MT
  • 1,224
  • 1
  • 4
  • 10