Message Bus

Source code: event_collector.messagebus.py

This module implements a message bus used internally between the CollectorQueue and other components.

The MessageBus allows one component of the event-collector system to listen to messages or bus events emitted by other components without explicit tight coupling.

MessageBus classes

class event_collector.messagebus.Message

Message is a generic base class for a bus message.

class event_collector.messagebus.Command

Command is a generic base class for a bus command.

class event_collector.messagebus.MessageBus(msg_handlers: dict[type[T_Message], list[Callable[[T_Message, deque[BusEvent]], Awaitable[None]]]] | None = None, cmd_handlers: dict[type[T_Command], Callable[[T_Command, deque[BusEvent]], Awaitable[None]]] | None = None)

The message bus works by dispatching messages or commands to handlers.

A MessageBus instance is initialized with a set of MsgHandlers and CmdHandlers.

Components define subclasses of Message or Command and dispatch or publish these events through the message bus. For each message or command, the message bus will check if any message or command handlers are registered to listen to the events. If a handler is registered, the bus will call the handler function when the message or command is triggered by the “dispatch” method.

The CollectorQueue emits a set of Messages that your application can listen to. These are documented in the (queue) module.