The message-oriented middleware

Message-oriented middleware (MOM) sends and receive messages between distributed systems. MOM allows application components to be distributed over heterogeneous platforms and reduces the complexity of developing such applications. The middleware creates a distributed communications layer that insulates the application developer from the details of the various network interfaces. It is a typical component of the microservice architecture, used for asynchronous tasks, complements synchronous HTTP REST API.

MOM is typically integrated with Message Queue servers such as RabbitMQ or Kafka. Messages are distributed thru these systems from and to various brokers. A message routing mechanism can be added to MQ server to steer a flow of the messages, if needed.

More theory can be found here: https://en.wikipedia.org/wiki/Message-oriented_middleware

MOM Service

class asab.mom.service.MOMService[source]

Message-oriented middleware is provided by a MOMService in a asab.mom module.

Service initialization and localization example:

from asab.mom import Module
self.add_module(Module)
svc = self.get_service("asab.MOMService")

Broker

class asab.mom.broker.Broker[source]

The broker is an object that provides methods for sending and receiving messages. It is also responsible for a underlaying transport of messages e.g. over the network to other brokers or MQ servers.

A base broker class Broker cannot be created directly, see available brokers below. Broker creating example:

from asab.mom.amqp import AMQPBroker
broker = AMQPBroker(app, config_section_name="bsfrgeocode:amqp")

Note: MOM Service has to be initialized.

Sending messages

Broker.publish(self, body, target:str='', correlation_id:str=None)[source]

Publishe the message to a MQ server.

message = "Hello World!"
await broker.publish(message, target="example")

Receiving messages

Broker.subscribe(subscription:str)[source]

Subscribe the broker to a specific subscription (e.g. topic or queue) on the MQ server. Once completed, messages starts to flow in and they are routed based on the target.

Broker.add(target:str, handler, reply_to:str=None)[source]

A message handler must be a coroutine that accept properties and body of the incoming message. Incoming messages are routed based on their target to a specific handler. If there is no registered handler for a target, the message is discarted.

broker.subscribe("topic")
broker.add('example', example_handler)

async def example_handler(self, properties, body):
        print("Recevied", body)

Replying to a message

Message-oriented middleware is the asynchronous message passing model. By a mechanism of a message correlation, MOM service allow to reply to a message in the handler.

Example of the handler:

async def example_handler(self, properties, body):
        print("Recevied", body)
        return "Hi there too"

Available brokers

class asab.mom.amqp.AMQPBroker