Custom Handlers

Custom handlers can be created and added to runners to build highly specialized actions, if needed.

Creating Event Handlers

Event handlers are the most commonly used type of handlers, which can be used to perform event steps within a saga flow. All event handlers extend the abstract EventHandler class.

package com.example.handler;

import java.util.Map;

import com.rierino.core.message.Event;
import com.rierino.core.util.HotException;
import com.rierino.runner.EventRunner;
import com.rierino.handler.EventHandler;

public class MyEventHandler extends EventHandler{

    {
        /**
         * actionMap provides a mapping between action names
         * (as used in sagas) and the functions called in handlers.
         * It is recommended to use universally unique action names 
         * across handlers to allow easy customization of UIs by action. 
         * Any number of entries can be added to actionMap, in case the
         * handler will support more than one actions. 
         */
        actionMap.put("MyAction", this::myAction);
    }

    /** 
     * Constructor with provided "alias", event runner which owns the handler
     * as well as all the configurations available to the runner
     */
    public MyEventHandler (String name, EventRunner runner, Map<String, String> config) {
        super(name, runner, config);
    }
    
    /**
     * Action function that is mapped to "MyAction" in actionMap, processing
     * an inputEvent and returning a produced outputEvent.
     * The function can simply throw a HotException for errors, 
     * which is automatically handled to return an event with failed status.
     * Within the function, other runner elements (such as state managers)
     * can be accessed for accessing configuration details.
     */
    public Event myAction(Event inputEvent) throws HotException {
        //TODO: utilize inputEvent to produce and return an outputEvent
    }

}

Dynamic handlers can be also used with the same class structure for real-time compilation using OpenHFT event handlers.

Creating Role Handlers

Role handlers are similar to event handlers, but are able to process less structured role data, instead of events, and are directly triggered from streams, instead of being used in sagas.

In most cases, EventHandlers are preferred, as they provide more flexibility with event metadata configurations.

RoleHandler is the parent class of EventHandler, so EventHandlers can also process role data if needed.

Optional Functions

Handlers provide a number of additional functions for their lifecycle management, which are triggered by the event runner during initialization, rebuild, shutdown and commit stages.

Optional Commands

Similar to role data and events, handlers are capable of receiving and responding to commands, such as reloading data from states (typically sent through async channels such as Kafka topics).

Supporting Features

All handlers have access to a variety of capabilities for processing events, role data and commands. Most frequently used features are as follows:

Variable Injection

Allows injection of event metadata, replacing variables that are provided with %%name%% notation (typically in saga event step configurations). Entire events can be injected with the following lines:

Output Caching

It is possible to make any handler cacheable by using available runWithCache function as a wrapper to output produced. Caching can be performed with the following lines:

Input / Output Mapping

Handlers typically consume certain sections of event payloads and store their outputs to a specific path on the payload. These mappings are facilitated through certain utility functions as follows:

Input Mapping

Output Mapping

Access to Data Sources

Handlers can access data sources - such as database tables - through state manager and query manager elements available within their event runners.

During Configuration

When state managers are used as sources of configuration (e.g. query definitions read from a database for QueryEventHandler), they are typically mapped in configure() function:

Some handlers use state managers to produce their own assets for operations (such as pre-compiled Handlebars templates), which require real-time tracking of updates in state managers to keep these assets in synch. This is typically also performed in configure() function, using state listeners:

During Actions

They can also be used inside the action functions, for reading / writing records on specific state managers:

Access to System Configurations

Some handlers use system configurations to access details of how to communicate with target systems (such as ModelScoreEventHandler for configuring access to model file system). These details are accessible through configuration loader as follows:

Last updated