-
Notifications
You must be signed in to change notification settings - Fork 6
ActorHost
ActorHost is responsible for passing incoming messages to actors for processing and sending outgoing messages as the result of their work. In order for an actor to start processing messages, it should be assigned to an ActorHost. This is done with the help of ActorHostManager:
// Create Composer instance, which will build other kino components
Composer composer = new Composer();
// Resolve required dependencies
RouterConfiguration routerConfiguration = new RouterConfiguration {...};
// Create an instance of your logger, which implements ILogger interface
ILogger logger = new MyLogger();
IActorHostManager actorHostManager = composer.BuildActorHostManager(routerConfiguration,
logger);
IActor actor = new MyActor();
actorHostManager.AssignActor(actor);
One ActorHost instance cannot register multiple actors to process the same message. Therefore, to make registration process easy, ActorHostManager takes care about finding the most suitable ActorHost instance for each actor (or create one if it doesn't exist yet). By default, actor will be assigned to one of the existing ActorHosts, if possible. Nevertheless, if you pass as a second parameter to the AssignActor() call the value ActorHostInstancePolicy.AlwaysCreateNew, actor will always be assigned to a newly created ActorHost instance. Because ActorHost uses only one thread to dispatch messages to all assigned actors, this might be useful if you would like to scale out processing by utilizing more threads. This is all about ActorHostManager.
For each incoming message, ActorHost looks up its ActorHandlerMap by message type to find registered handler function. If handler is not found (which should not really happen), the message is not processed and just dies. Otherwise, the message handler function is executed. Depending on whether the handler returns result synchronously or asynchronously, the response message is sent immediately or Task is added into ActorHosts's completion queue. Unhandled exceptions from handler function are converted into ExceptionMessage and sent as the result.