Azure Service Bus sessions - establish receive session on empty queue

srisa 21 Reputation points
2021-11-24T21:09:10.683+00:00

I am reading the documentation and the samples for the Azure Service Bus queue with sessions.

One thing that is unclear: Will I be able to create get a lock on a session if there have not yet been any messages sent to the queue with that specific session id?

E.g. the example code shows:

// The Mono completes successfully when a lock on the session is acquired, otherwise, it completes with an
// error.
Mono<ServiceBusReceiverAsyncClient> receiverMono = sessionReceiver.acceptSession("greetings-id");

What happens if there currently are no messages (or have never been sent any messages) on the queue with the "greetings-id" sessionid? Will it fail to aquire a session then? Or will it aquire the lock so that I can process any future messages arriving with that session id?

A normal scenario for me, will be to establish the session, and "wait for the first message to arrive" with that session id.

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
542 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 68,391 Reputation points
    2021-11-26T08:39:01.257+00:00

    @srisa When your client calls the acceptSession method then all the messages that will be sent using this session will only be consumed by that client.
    Let's say if you don't have any message in the queue and the client called acceptSession("greetings-id") then your client will keep on waiting for the new messages that will be sent with the session id greetings-id.

    In case if there is no message with that session ID then there will be no expected message raised as it waits for the messages for that session id. Once your client found a new message (with greetings-id) it will hold the lock and consume that message.

    The same is covered on service bus session document.

    A session receiver is created by a client accepting a session. When the session is accepted and held by a client, the client holds an exclusive lock on all messages with that session's session ID in the queue or subscription. It will also hold exclusive locks on all messages with the session ID that will arrive later.

    Feel free to get back to me if you have any queries or concerns.