Azure service Bus Exception on trye to complete message
Asked Answered
S

2

1

I'm using the GitHub example to process the messages of a topic:

private void RegisterSubscriptionClientMessageHandler()
        {
            _subscriptionClient.RegisterMessageHandler(
                async (message, token) =>
                {
                    var eventName = $"{message.Label}{INTEGRATION_EVENT_SUFIX}";
                    var messageData = Encoding.UTF8.GetString(message.Body);
                    await ProcessEvent(eventName, messageData);

                    // Complete the message so that it is not received again.
                    await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
                },
               new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 10, AutoComplete = false });
        }

All messages are sent and received by all subscribers successfully, however in the command:

await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);

The following error always occurs:

 ERROR ON ExceptionReceivedHandler EXEPTION: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance.
Bazinga.EventBus.Bus.EventBusSubscription:Error: ERROR ON ExceptionReceivedHandler EXEPTION: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance.
- Executing Action: UserCallback

Any tips on how to solve? Thank you!

Sinistrality answered 4/9, 2018 at 14:58 Comment(5)
Are you sure the message are still in the queue? The error message says there are no messages in queuesIdealize
@Idealize - Thank you and good question: How can I verify this accurately and effectively?Sinistrality
I've provided my answer. Additionally, for your question, you can check the Azure portal to check whether your messages were in the queues/topics (or) you can use Azure Service Bus Explorer to verify thatIdealize
Any update? Did you find the solution useful then upvote/accept the answer,so that it will to help the communityIdealize
Sorry for the delay ... none of the solutions posted worked. I still have the same problem. I'm studying the documentation to try to figure out what I'm doing wrong. Keep you informed. Happy Codding ;-)Sinistrality
S
1

When a message is received from Service Bus Queue or Topic Subscription, a lock token will be returned with the message.

The lock token can be used to delete, dead letter or defer a message.

Every Queue and Topic Subscription has Lock duration property with it. Based on the time span configured for Lock duration, the lock supplied with the message will be expired.

Here, you are doing some processing await ProcessEvent(eventName, messageData); before completing the message.

The problem must be the the lock gets expired before_subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); line is executed.

Increasing the lock duration or completing the message before calling ProcessEvent will solve your problem.

Slender answered 4/9, 2018 at 15:14 Comment(2)
I will test now and return to you. Thanks.Sinistrality
Hey @arunprabhu can you please explain what is the purpose of the lock duration? in which cases do I need to set it to max (5 min) and how its corresponds to the duration of ProcessEvent? if the duration is longer than the process duration? and when is not.Wivina
I
1

The other main reason for this will be Autocomplete is enabled by default.

Whenever you are using CompleteAsync() then you should also instantiate an OnMessageOptions object in order to set the set AutoComplete to false, and pass it into your OnMessage call.

OnMessageOptions onMessageOpt = new OnMessageOptions();
onMessageOpt.AutoComplete = false;

client.OnMessage(processCalculations, options);

See this Similar SO Solution

Idealize answered 4/9, 2018 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.