Skip to content

Distributed messaging

Ioan Crișan edited this page Oct 23, 2020 · 12 revisions

The message processing is an in-process feature, which is not per se bad, but in distributed scenarios it is limiting. The distributed messaging fills the gap by providing infrastructure components that can communicate in a such an environment.

Using the messaging infrastructure is a little more elaborate:

    // ensure the message has been sent
    await messageBroker.DispatchAsync(new RefreshCacheEvent { Key = "Users" }, ctx => ctx.OneWay()).PreserveThreadContext();   

This can be achieved in a simpler way, using the provided extension methods:

    // ensure the message has been sent
    await messageBroker.PublishAsync(new RefreshCacheEvent { Key = "Users" }).PreserveThreadContext();   

The distributed messaging flow

Distributed Messaging

The participants in the distributed message flow are:

  • The brokered message: This is the message being sent through the underlying infrastructure to the processors.
  • The message broker: This is the in-process component dispatching the messages by the means of the underlying infrastructure.
  • The brokered message handler: This is the message handler on the other end, receiving the brokered message and ensuring that it is processed and, if requested, a proper response is sent back.
  • Infrastructure dependent components which:
    • send/queue messages.
    • read the messages and forward them to the in-process message processor .

Brokered message

A brokered message (IBrokeredMessage) is a specialization of a message carrying with it the original message to be processed and some distributed environment information. It can be regarded as an "envelope" transporting the actual message.

  • Content: the message to be actually processed.
  • Sender: contains information about the message sender. For some broadcasts, it can be used to not forward the message to the original sender.
  • Channel: contains the channel to use. If not provided, the default channel will be used.
  • IsOneWay: indicates whether the sender awaits an answer to the message, or is just a fire and forget scenario.
  • Timeout: the timeout for requests awaiting for a response.

The message broker

This is a shared application service dispatching messages through a channel to registered processors.

The IMessageBroker service contract has the following methods:

  • DispatchAsync(object message, [optionsConfig: Action<IDispatchingContext>], [cancellationToken: CancellationToken]): Task<IMessage>: Dispatches the brokered message asynchronously.
    • Depending on the IsOneWay setting in the brokered message, it waits for a response or not.

InProcessMessageBroker

The in-process message broker is the Kephas implementation for a message broker dispatching the message to the in-process message processor. It has the lowest override priority.

This is the default message broker used when no custom one has been defined. It is recommended to use real-world implementations, using established message queuing infrastructure.

The brokered message handler

Brokered messages are nothing more than message requiring proper handling. The brokered message handler is the default implementation forwarding the contained message to the in-process message processor.

Clone this wiki locally