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.
Constructor
ServiceBusSession(session_id: str, receiver: ServiceBusReceiver | ServiceBusReceiverAsync)
Parameters
| Name | Description |
|---|---|
|
session_id
Required
|
|
|
receiver
Required
|
|
Examples
Get session from a receiver
with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
session = receiver.session
Variables
| Name | Description |
|---|---|
|
auto_renew_error
|
<xref:azure.servicebus.AutoLockRenewTimeout> or
<xref:azure.servicebus.AutoLockRenewFailed>
Error when AutoLockRenewer is used and it fails to renew the session lock. |
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.AutoLockRenewer instance. |
| set_state |
Set the session state. |
get_state
Get the session state.
Returns None if no state has been set.
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. Default value: None
|
Returns
| Type | Description |
|---|---|
|
The session state. |
Examples
Get the session state
with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
session = receiver.session
session_state = 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.AutoLockRenewer instance.
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. Default value: None
|
Returns
| Type | Description |
|---|---|
|
The utc datetime the lock is set to expire at. |
Examples
Renew the session lock before it expires
with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
session = receiver.session
session.renew_lock()
set_state
Set the session state.
set_state(state: str | bytes | bytearray | None, *, timeout: float | None = None, **kwargs: Any) -> None
Parameters
| Name | Description |
|---|---|
|
state
Required
|
The state value. Setting state to None will clear the current session. |
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. Default value: None
|
Returns
| Type | Description |
|---|---|
|
None |
Examples
Set the session state
with servicebus_client.get_queue_receiver(queue_name=queue_name, session_id=session_id) as receiver:
session = receiver.session
session.set_state("START")