Closed h5chauhan closed 5 years ago
First you shouldn't swallow the exception on complete. If complete fails, you will receive the same message again. MessageLockLostException rarely gets thrown when you complete it in time. Please log/trace the lock token and LockedUntilUtc time. If you are getting one too many LockLost exceptions even within LockedUntilUtc, I will investigate further.
val sharedAccessKey: String = "secret"
val entityPath: String = "my-entity-path"
val accessURI: URI = URI.create("sb://my-service.servicebus.windows.net/")
val sharedAccessKeyName = "All"
val fw = new FileWriter("output.txt", true)
val receiver = ClientFactory.createMessageReceiverFromConnectionStringBuilder(new ConnectionStringBuilder(accessURI,
entityPath, sharedAccessKeyName, sharedAccessKey))
receiver.setPrefetchCount(10)
val objectMapper = new ObjectMapper()
while (true) {
val messages = receiver.receiveBatch(10)
if (messages != null) {
messages.asScala.foreach { m =>
val body = new String(m.getBody)
val properties = m.getProperties
val tree = objectMapper.readTree(properties.get("JsonData"))
val jsonString = objectMapper.writeValueAsString(tree)
fw.append(jsonString)
fw.append("\n")
fw.flush()
receiver.complete(m.getLockToken)
}
} else {
println("Message is null")
}
}
}
Here is what i am doing to reproduce this the error happens on the receiver.complete() call.
2018-10-29 18:58:12,305 WARN || Received delivery '????4CB?,???' state 'Rejected{error=Error{condition=com.microsoft:message-lock-lost, description='The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. Reference:f27dfe2e-091c-4dc9-a1db-f2fcd5a5f4a4, TrackingId:70433eac0000315f000000595bd75640_G23_B6, SystemTracker:partner-prod:queue:my-queue~207, Timestamp:10/29/2018 6:58:12 PM', info=null}}' doesn't match expected state 'Accepted{}' [com.microsoft.azure.servicebus.primitives.CoreMessageReceiver]
I wonder if this will give more context to understand the issue?
what is the lock duration on your entity? Let's say you receive a batch of 10 messages. All messages will have the same lock duration. And you are completing them sequentially one after the other. So the last message in your batch will be completed after the first 9 messages are processed and completed. Depending upon how long processing each message takes, the last one's lock might have expired by the time you call complete for that message.
There are some options you can try..
Describe the bug We are seeing the below error when completing the messages received with
receiver.receiveBatch()
call.We have tried disabling prefetch to no effect. The batch size is set between 50-100. Example of the way we create client is shown below -
Expected behavior The above code swallows the exception mentioned in order to continue processing but this causes duplicates in our system.
Environment (please complete the following information):