ServiceBusSession Class

The ServiceBusSession is used for manage session states and lock renewal.

Please use the property session on the ServiceBusReceiver to get the corresponding ServiceBusSession object linked with the receiver instead of instantiating a ServiceBusSession object directly.

Inheritance
azure.servicebus._servicebus_session.BaseSession
ServiceBusSession

Constructor

ServiceBusSession(session_id: str, receiver: ServiceBusReceiver | ServiceBusReceiverAsync)

Parameters

Name Description
session_id
Required
receiver
Required

Examples

Get session from a receiver


       async with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
           session = receiver.session

Methods

get_state

Get the session state.

Returns None if no state has been set.

renew_lock

Renew the session lock.

This operation must be performed periodically in order to retain a lock on the session to continue message processing.

Once the lock is lost the connection will be closed; an expired lock cannot be renewed.

This operation can also be performed as a threaded background task by registering the session with an azure.servicebus.aio.AutoLockRenewer instance.

set_state

Set the session state.

get_state

Get the session state.

Returns None if no state has been set.

async get_state(*, timeout: float | None = None, **kwargs: Any) -> bytes

Keyword-Only Parameters

Name Description
timeout

The total operation timeout in seconds including all the retries. The value must be greater than 0 if specified. The default value is None, meaning no timeout.

Returns

Type Description

The session state.

Examples

Get the session state


       async with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
           session = receiver.session
           session_state = await session.get_state()

renew_lock

Renew the session lock.

This operation must be performed periodically in order to retain a lock on the session to continue message processing.

Once the lock is lost the connection will be closed; an expired lock cannot be renewed.

This operation can also be performed as a threaded background task by registering the session with an azure.servicebus.aio.AutoLockRenewer instance.

async renew_lock(*, timeout: float | None = None, **kwargs: Any) -> datetime

Keyword-Only Parameters

Name Description
timeout

The total operation timeout in seconds including all the retries. The value must be greater than 0 if specified. The default value is None, meaning no timeout.

Returns

Type Description

The utc datetime the lock is set to expire at.

Examples

Renew the session lock before it expires


       async with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
           session = receiver.session
           await session.renew_lock()

set_state

Set the session state.

async set_state(state: str | bytes | bytearray | None, *, timeout: float | None = None, **kwargs: Any) -> None

Parameters

Name Description
state
Required
str or bytes or bytearray or None

The state value.

Keyword-Only Parameters

Name Description
timeout
float or None

The total operation timeout in seconds including all the retries. The value must be greater than 0 if specified. The default value is None, meaning no timeout.

Returns

Type Description
any

Response of callback

Examples

Set the session state


       async with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
           session = receiver.session
           await session.set_state("START")

Attributes

locked_until_utc

The time at which this session's lock will expire.

Returns

Type Description

session_id

Session id of the current session.

Returns

Type Description
str