Cratis Chronicle reactors: create your first handler
A Cratis Chronicle reactor connects a recorded event to application work, such as sending an order notification. Define the event it handles, add the handler, then register the reactor with your client so Chronicle can deliver events to it.
Start with a running Chronicle kernel and a connected client. If you still need those, follow Get started with Cratis Chronicle first. The examples below show reactor declarations and registration, not a complete application.
Use a projection or reducer instead when you need queryable state rather than a side effect.
Define the event
Section titled “Define the event”Choose the fact that should trigger the work. The order-placed event below carries the information the notification handler needs. It is a normal Chronicle event with its own event type identity, not a special message format for reactors.
[EventType]public record ReactorOrderPlaced(string CustomerEmail, decimal TotalAmount);import io.cratis.chronicle.events.EventType
@EventTypedata class ReactorOrderPlaced( val customerEmail: String, val totalAmount: Double)import io.cratis.chronicle.events.EventType;
@EventTyperecord ReactorOrderPlaced(String customerEmail, double totalAmount) {}defmodule MyApp.Events.ReactorOrderPlaced do use Cratis.Chronicle.Events.EventType, id: "reactor-order-placed-v1"
defstruct [:customer_email, :total_amount]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ReactorOrderPlaced { @field(String) readonly customerEmail: string; @field(Number) readonly totalAmount: number;
constructor(customerEmail: string, totalAmount: number) { this.customerEmail = customerEmail; this.totalAmount = totalAmount; }}Define the reactor
Section titled “Define the reactor”Add the handler to a reactor type or module. The client determines how it finds that handler: .NET uses the first parameter’s event type, Kotlin and Java use event-typed methods, TypeScript supports event-name conventions, and Elixir uses @handles with handle/2. Use the event context when the work needs metadata such as the event source ID or sequence number.
public interface ReactorEmailGateway{ Task SendOrderPlaced(string email, decimal amount, DateTimeOffset occurred);}
public class OrderNotificationsReactor(ReactorEmailGateway emailGateway) : IReactor{ public Task Placed(ReactorOrderPlaced @event, EventContext context) => emailGateway.SendOrderPlaced( @event.CustomerEmail, @event.TotalAmount, context.Occurred);}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.observation.Reactorimport java.time.Instant
interface ReactorEmailGateway { fun sendOrderPlaced(email: String, amount: Double, occurred: Instant)}
@Reactorclass OrderNotificationsReactor(private val emailGateway: ReactorEmailGateway) { fun placed(event: ReactorOrderPlaced, context: EventContext) { emailGateway.sendOrderPlaced( event.customerEmail, event.totalAmount, context.occurred) }}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.observation.Reactor;import java.time.Instant;
interface ReactorEmailGateway { void sendOrderPlaced(String email, double amount, Instant occurred);}
@Reactorclass OrderNotificationsReactor { private final ReactorEmailGateway emailGateway;
OrderNotificationsReactor(ReactorEmailGateway emailGateway) { this.emailGateway = emailGateway; }
void placed(ReactorOrderPlaced event, EventContext context) { emailGateway.sendOrderPlaced( event.customerEmail(), event.totalAmount(), context.getOccurred()); }}defmodule MyApp.EmailGateway do def order_placed(_email, _amount, _occurred), do: :okend
defmodule MyApp.Reactors.OrderNotificationsReactor do use Cratis.Chronicle.Reactors.Reactor
alias MyApp.Events.ReactorOrderPlaced
@handles ReactorOrderPlaced
@impl true def handle(%ReactorOrderPlaced{} = event, context) do MyApp.EmailGateway.order_placed( event.customer_email, event.total_amount, Map.get(context, :occurred))
:ok endendimport { EventContext, onceOnly, reactor } from '@cratis/chronicle';
interface ReactorEmailGateway { sendOrderPlaced(email: string, amount: number, occurred: Date): Promise<void>;}
@reactor()class OrderNotificationsReactor { constructor(private readonly emailGateway: ReactorEmailGateway) {}
// Without @handles(EventType), the method name must be the exact camelCase of the // event's class name - Chronicle discovers undecorated handlers by name, not by parameter type. @onceOnly() async reactorOrderPlaced(event: ReactorOrderPlaced, context: EventContext): Promise<void> { await this.emailGateway.sendOrderPlaced( event.customerEmail, event.totalAmount, context.occurred); }}Register the reactor
Section titled “Register the reactor”Register the reactor in the application that will run its handler. The .NET host can discover reactors at startup; the examples also show explicit registration. Kotlin and Java register an instance, TypeScript registers discovered reactor classes, and Elixir includes the module in client startup options.
public class ReactorRegistration{ public Task Register(IEventStore eventStore) => eventStore.Reactors.Register();}import io.cratis.chronicle.IEventStoreimport kotlinx.coroutines.Job
class ReactorRegistration(private val emailGateway: ReactorEmailGateway) { suspend fun register(store: IEventStore): Job = store.reactors.register(OrderNotificationsReactor(emailGateway))}import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.java.BlockingEventStore;import kotlinx.coroutines.Job;
class ReactorRegistration { private final ReactorEmailGateway emailGateway;
ReactorRegistration(ReactorEmailGateway emailGateway) { this.emailGateway = emailGateway; }
Job register(IEventStore store) { return new BlockingEventStore(store) .getReactors() .register(new OrderNotificationsReactor(emailGateway)); }}defmodule MyApp.ReactorClientConfig do def child_spec do {Cratis.Chronicle.Client, connection_string: "chronicle://localhost:35000", event_store: "store", reactors: [MyApp.Reactors.OrderNotificationsReactor]} endendimport { IEventStore } from '@cratis/chronicle';
class ReactorRegistration { async register(store: IEventStore): Promise<void> { await store.reactors.register(); }}Use a stable reactor ID
Section titled “Use a stable reactor ID”Give the reactor an explicit ID before deploying it. Clients otherwise derive the identifier from the type or module name, so a rename can make Chronicle see a different observer. Keeping the ID stable preserves the connection to its stored observation position.
[Reactor(id: "order-notifications")]public class NamedOrderNotificationsReactor : IReactor{ public Task Placed(ReactorOrderPlaced @event) => Task.CompletedTask;}import io.cratis.chronicle.observation.Reactor
@Reactor(id = "order-notifications")class NamedOrderNotificationsReactor { fun placed(event: ReactorOrderPlaced) { // Perform the side effect. }}import io.cratis.chronicle.observation.Reactor;
@Reactor(id = "order-notifications")class NamedOrderNotificationsReactor { void placed(ReactorOrderPlaced event) { // Perform the side effect. }}defmodule MyApp.Reactors.NamedOrderNotificationsReactor do use Cratis.Chronicle.Reactors.Reactor, id: "order-notifications"
alias MyApp.Events.ReactorOrderPlaced
@handles ReactorOrderPlaced
@impl true def handle(%ReactorOrderPlaced{}, _context), do: :okendimport { onceOnly, reactor } from '@cratis/chronicle';
@reactor('order-notifications')class NamedOrderNotificationsReactor { // Without @handles(EventType), the method name must be the exact camelCase of the // event's class name - Chronicle discovers undecorated handlers by name, not by parameter type. @onceOnly() async reactorOrderPlaced(_event: ReactorOrderPlaced): Promise<void> { // Perform the side effect. }}Design for replay
Section titled “Design for replay”A newly registered reactor normally catches up on existing history, so a notification handler can run for old orders as well as new ones. If it should handle only future events, read Start at the Tail before deploying it. Excluding replay alone does not exclude initial catch-up.
The same event can also arrive again during recovery or retry. Make the effect idempotent: repeating the operation must not repeat its business outcome. For an external API, pass an idempotency key that the receiving system enforces; Delivery Identity explains the identity Chronicle supplies.
With the reactor registered, append an event of the type it handles and check the handler’s effect. If it does not run, inspect the reactor’s state and failed partitions before appending the event again.
Next steps
Section titled “Next steps”Read Event Processing when your handler needs context or dependencies, or Filtering when only some events of a handled type should reach it. Tagging reactors labels observers for organization; it does not filter delivery.
For the data-building alternative, compare reducers and projections.