ServiceBusTrigger consume all active messages instead of newly created message

Saranraj C 0 Reputation points
2024-01-10T06:03:33.72+00:00

Hi this is Saran.

We are using azure service bus. And producing message to service bus topic.

We have created one azure function ServiceBusTrigger for consuming message from service bus topic. ServiceBusTrigger is working when ever new message produced in topic. But ServiceBusTrigger function consuming all active messages instead of newly created one. We want to consume only new message from topic.

For example:

Consider already 10 active message available in topic.

Now we are producing message number 11.

That time ServiceBusTrigger trigger working but it fetching all 11 messages instead of fetching last message (11th message).

Kindly give you suggestion to consume only new message.

Thanks.

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,930 questions
Developer technologies | .NET | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Ben Gimblett 4,560 Reputation points Microsoft Employee
    2024-01-10T11:58:43.2766667+00:00

    Hi - Thanks for the question, the problem here is in the way you are asking SB trigger to handle the message

    [ServiceBusTrigger("Topic_CA","Subscription_CA", Connection = "ServiceBusConnectionString_CA",AutoComplete =false)]
    

    This is the problem

    AutoComplete =false
    

    There are in effect two modes of operation

    (1) the default is the message handed to your function code has a "peek lock" assigned. Depending on how the code finishes, when the function exits the lock is either completed (the message is removed as it's been processed) or the lock is removed so another instance can retry the message processing (for example the function threw an exception)

    (2) auto complete - you can opt out of using peek lock and auto-complete BUT if you do this you need to complete the message (or abandon it) manually in code yourself. I suspect you are not doing this. Not doing this means you see the same messages over and over again as they never get removed

    Answer is also explained here https://stackoverflow.com/questions/62752905/azure-function-service-bus-trigger-running-multiple-times


  2. MayankBargali-MSFT 70,941 Reputation points Moderator
    2024-01-17T10:09:23.17+00:00

    Saranraj C Thanks for reaching out. The azure function service bus or any service bus SDK consumes all the messages that are present in a particular subscription. Unfortunately, there is no out of box way to consume the messages from a particular number etc. In case if you know which messages to consume then you can abandoned those messages so those can be discarded or moved to the dead letter queue as per your service bus entity configuration. Ben has already shared some of the reference documents.

    Let me know if you have any queries or concerns.

    Please 'Accept Answer' if it helped so that it can help others in the community looking for help on similar topics.

    0 comments No comments

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.