Working with Xafari (x07) Message Queue | Xafari Framework

Xafari Server uses its own implementation of the Message Queue. Clients add tasks (hereinafter messages) to the Queue and the Server processes messages from the Queue. This post provides an example of how to implement client-server interaction via the Message Queue.

Message Queuing work includes the following stages:

  1. The client adds the message to the Queue, the message is decorated with a specified tag. The message is saved in the database.
  2. Xafari Server retrieves the message from the database, finds a handler with the corresponding tag and starts processing the message.
  3. The handler can save the results of their work to the database. The client can get them for further work.

What required, to use Message Queue

To use Message Queue, execute the following steps:

  1. Add Xafari.MQ and Xafari.Server modules to your module.
  2. Define the data type for the message. Create and send a message by calling Instance.InsertMessage<T> method. Also, specify the tag of the message handler.
  3. Define a message handler class, it must implement the IMessageHandler interface. Then register a message handler class on the server. Necessarily specify a tag, each type of message corresponds to a single unique tag. Tag binds message and handler.


Let's develop XafariMQSample solution to examine the Xafari Message Queue. We will create a controller containing AddMessageToQueue Action, that will add a message to the queue. Message have the Index parameter indicating the number of the call Action. After the message is processed, the MessageLog object will be created and stored in the database.


Xafari.MQ message is anĀ MQMessage Domain Component. The MessageData property stores the message data. It is a non-typed reference property of the XPWeakReferenceStruct type. In this example, the message data type will contain only one property.

To add a message to the queue, use the MQManager.InsertMessage<T>(T messageData, string tag, string description) method.

The first parameter specifies the message data, it will be transferred to an instance of the Xafari Server. Tag parameter specifies a tag to find the required handler. The method returns the message key to retrieve the result from the database.

MQManager class exposes CancelMessageExecution(MQMessage mqMessage) method, to cancel the processing of the message. You can cancel only the message that has not yet been processed, i.e. CurrentStatus is "WaitingForExecution".

MessageData class represents the message data of XafariMQSample application.

To add a message to Message Queue use following code:

Note: to work with messages it is necessary to use MQManager ObjectSpace returned by the MQManager.Instance.CreateObjectSpace() method.

Message Handler

To create a message handler you need to implement IMessageHandler interface. Then you need to register the handler by calling RegisterHandler(string tag, Func<XafApplication, object, IMessageHandler> handlerCreator) method.

The first parameter is a handler tag. For one tag, you can register only one handler. It will be called by Xafari Server to create a handler instance.

The second parameter is a delegate returns IMessageHandler implementation. The first parameter of the delegate specifies the Application, i.e. server runs under the specified Application. The second parameter (object type) specifies a message key, handler instance will retrieve a target message from the database using the key.

Handler registration:

When implementing IMessageHandler you have to take into account some of the nuances:

  1. Raise OnStartedThread event before the core code of handler. Accordingly, raise OnCompleted and OnStoppedThread events after the end of the core code.
  2. To work with messages use MQManager ObjectSpace.
  3. When implement StartHandler() method immediately block the message by setting its status to "Processing" (see LockReportMessage() method in the sample).

The code snippet below demonstrates the handler implementation:

Load solution: XafariMQSample.