activation

Message handler activation module.

This module provides classes for registering and activating message handlers based on message types. The handler activator pattern is core to how Mersal dispatches messages to the appropriate handlers.

class mersal.activation.BuiltinHandlerActivator

Bases: object

Default implementation of the handler activator pattern.

This class manages the registration and activation of message handlers based on message types. It stores handler factories indexed by message type and instantiates the appropriate handlers when a message needs to be processed.

__init__() None

Initialize a new instance of the BuiltinHandlerActivator.

property app: Mersal

Get the associated application instance.

Returns:

The Mersal application instance

async get_handlers(message: MessageT, transaction_context: TransactionContext) Sequence[MessageHandler[MessageT]]

Get handlers for the specified message.

Parameters:
  • message – The message to get handlers for

  • transaction_context – The current transaction context

Returns:

A sequence of message handlers that can process the message

Raises:

Exception – If called outside of a message context

register(message_type: type[MessageT], factory: HandlerFactory[MessageT]) BuiltinHandlerActivator

Register a handler factory for a specific message type.

Parameters:
  • message_type – The type of message to register a handler for

  • factory – A factory function that creates a message handler

Returns:

The handler activator instance for method chaining

property registered_message_types: set[type]

Get the set of message types that have registered handlers.

Returns:

A set containing all registered message types

class mersal.activation.HandlerActivator

Bases: Protocol

Protocol defining the interface for activating message handlers.

The HandlerActivator is responsible for registering message handlers and returning the appropriate handlers for a given message type during message processing.

__init__(*args, **kwargs)
async get_handlers(message: MessageT, transaction_context: TransactionContext) Sequence[MessageHandler[MessageT]]

Retrieve handlers for the given message.

Parameters:
  • message – The message to get handlers for

  • transaction_context – The current transaction context

Returns:

A sequence of message handlers that can process the message

register(message_type: type[MessageT], factory: HandlerFactory[MessageT]) HandlerActivator

Register a handler factory for a specific message type.

Parameters:
  • message_type – The type of message to register a handler for

  • factory – A factory function that creates a message handler

Returns:

The handler activator instance for method chaining

property registered_message_types: set[type]

Get the set of message types that have registered handlers.

Returns:

A set containing all registered message types