简体   繁体   中英

Azure ServiceBus managing of session-based receivers

When using session-based entities for Azure Service Bus, what are the best practices for managing the receivers? I'm managing my non-session-based entities through pooling, similar to what is recommended here . Could session-based entities be pooled in the same way, perhaps together with the SessionId ?
Currently, I'm creating/disposing the receiver instance for every call, although the session is expected to receive multiple messages in close succession.

The receiver is instantiated as follows:

await using var receiver = await _serviceBusFactory.ServiceBusClient.AcceptSessionAsync(topicName, subscriptionName, sessionId);

You could pool them as in the non-session example, but there is a caveat: if the session receiver loses the session lock you would need to accept the session again, so you would likely want to attempt some operation with the session receiver before handing it back to the caller to ensure that the lock is still valid, such as calling GetSessionStateAsync. If the lock is lost, you would get an exception. I think this could probably be improved by introducing a property on the SessionReceiver to indicate if it is effectively dead.

Alternatively, you could use the ServiceBusSessionProcessor which handles the session receiver management for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM