Skip to content
Sergey Ivasenko edited this page Apr 29, 2018 · 9 revisions

MessageHub is an entry point for a message into a kino network. You can use it for sending one way messages like this:

IPayload payload = message.GetPayload<MyMessage>();
IMessage message = Message.CreateFlowStartMessage(payload);

// Create Composer instance, which will build other kino components
var composer = new Composer();

// Resolve required dependencies
MessageHubConfiguration messageHubConfiguration = new MessageHubConfiguration {...};
// Create an instance of your logger, which implements ILogger interface
ILogger logger = new MyLogger();

// New instance is created every time BuildMessageHub() is called.
IMessagHub messageHub = composer.BuildMessageHub(messageHubConfiguration, logger);
messageHub.SendOneWay(message);

This is a simple case, when we are not expecting any result back.

Nevertheless, complex functionality may require interaction between many actors. These actors will send and receive different messages, executing the message flow defined by the logic of your application. Depending on the outcome of the message flow, i.e. failed, succeeded, etc., we might receive different message types representing the result, or return value of the flow. If you are interested in getting the result message, you can specify a CallbackPoint of the message flow like this:

IMessage request = Message.CreateFlowStartMessage(new StartMessage());
ICallbackPoint callbackPoint = CallbackPoint.Create<ResultMessage>();

In the code above, message flow starts with StartMessage. The result of the flow is represented by ResultMessage, which sent as a final step by some actor. Defining the callback point with ResultMessage, we tell framework to send this message back to the caller:

IMessage request = Message.CreateFlowStartMessage(new StartMessage());
ICallbackPoint callbackPoint = CallbackPoint.Create<ResultMessage>();

// Create Composer instance, which will build other kino components
var composer = new Composer();

// Resolve required dependencies
MessageHubConfiguration messageHubConfiguration = new MessageHubConfiguration {...};
// Create an instance of your logger, which implements ILogger interface
ILogger logger = new MyLogger();

// New instance is created every time BuildMessageHub() is called.
IMessagHub messageHub = composer.BuildMessageHub(messageHubConfiguration, logger);

using(IPromise promise = messageHub.EnqueueRequest(request, callbackPoint))
{
    if(promise.GetResponse().Wait(timeout))
    {
        ResultMessage result = promise.GetResponse().Result.GetPayload<ResultMessage>();
        ///
    }
}

If we would like to get the response back, we pass initial message and callback point to EnqueueRequest() method of MessageHub instance. EnqueueRequest() method returns reference to the IPromise object, which later will be resolved to the value of the callback message. Do not forget to dispose the promise object afterwards.

Using callbacks you can as well implement back pressure, if needed.

MessageRouter

Clone this wiki locally