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, receiver)

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

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(**kwargs)

Parameters

timeout
float

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.

Return type

str

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(**kwargs)

Parameters

timeout
float

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

The utc datetime the lock is set to expire at.

Return type

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, **kwargs)

Parameters

state
<xref:Union>[str, bytes, bytearray]
Required

The state value.

timeout
float

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.

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")