Cratis Chronicle reactors for event-driven side effects
Cratis Chronicle reactors handle recorded events to trigger application work, such as sending a notification or calling an external API. Use a reactor when that work belongs after an event has been stored, rather than in the code that appends it.
A reactor reads an event sequence and keeps its own observation position: the point it has reached in that sequence. Chronicle uses this position to resume processing after a restart.
Key Concepts
Section titled “Key Concepts”A handler is the application method that runs for an event type. Its event context supplies metadata such as the event source ID and sequence number. Each client has its own way to declare and register handlers; Getting Started shows the differences.
Events from one event source, such as one order, are handled in sequence order within their partition. Different partitions can progress independently. If handling fails, inspect and retry the failed partition rather than restarting the reactor’s entire history.
Replaying a reactor runs it against event history again. Decide which effects are safe to repeat before using that workflow. Once-only processing excludes work from replay, but does not prevent redelivery during recovery. Use delivery identity when protecting an external effect from repeated delivery.
When to Use Reactors
Section titled “When to Use Reactors”Reactors are a good fit when you need to:
- Trigger notifications or workflows when specific events occur
- Synchronize with external systems
- Start background processing after a business fact has been recorded
- Emit follow-up events based on patterns in the event stream
If you need a queryable view of event history instead, use a projection or reducer. Those build data to read; a reactor runs application code in response to a recorded fact.
Basic Example
Section titled “Basic Example”The example connects an event to a notification handler. Select your client to see its declaration and handler convention:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Reactors;
[EventType]public record ReactorsIndexEmailConfirmed(string Email);
public class ReactorsIndexEmailNotificationsReactor : IReactor{ public Task Confirmed(ReactorsIndexEmailConfirmed @event, EventContext context) => SendConfirmationAsync(@event.Email, context.Occurred);
Task SendConfirmationAsync(string email, DateTimeOffset occurred) => Task.CompletedTask;}import io.cratis.chronicle.events.EventContextimport io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.Reactor
@EventTypedata class ReactorsIndexEmailConfirmed(val email: String)
@Reactorclass ReactorsIndexEmailNotificationsReactor { fun confirmed(event: ReactorsIndexEmailConfirmed, context: EventContext) { sendConfirmation(event.email, context.occurred) }
private fun sendConfirmation(email: String, occurred: java.time.Instant) {}}import io.cratis.chronicle.events.EventContext;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.Reactor;import java.time.Instant;
@EventTyperecord ReactorsIndexEmailConfirmed(String email) {}
@Reactorclass ReactorsIndexEmailNotificationsReactor { void confirmed(ReactorsIndexEmailConfirmed event, EventContext context) { sendConfirmation(event.email(), context.getOccurred()); }
private void sendConfirmation(String email, Instant occurred) {}}defmodule MyApp.Events.ReactorsIndexEmailConfirmed do use Cratis.Chronicle.Events.EventType, id: "reactors-index-email-confirmed"
defstruct [:email]end
defmodule MyApp.Reactors.ReactorsIndexEmailNotificationsReactor do use Cratis.Chronicle.Reactors.Reactor
alias MyApp.Events.ReactorsIndexEmailConfirmed
@handles ReactorsIndexEmailConfirmed
@impl true def handle(%ReactorsIndexEmailConfirmed{} = event, context) do send_confirmation(event.email, Map.get(context, :occurred))
:ok end
defp send_confirmation(_email, _occurred), do: :okendimport { eventType, onceOnly, reactor, EventContext } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ReactorsIndexEmailConfirmed { @field(String) readonly email: string;
constructor(email: string) { this.email = email; }}
@reactor()class ReactorsIndexEmailNotificationsReactor { // 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 reactorsIndexEmailConfirmed(event: ReactorsIndexEmailConfirmed, context: EventContext): Promise<void> { await this.sendConfirmation(event.email, context.occurred); }
private async sendConfirmation(email: string, occurred: Date): Promise<void> {}}If you register artifacts explicitly in your .NET application’s setup code, you can nominate a plain instance-method reactor without IReactor. Dispatch and dependency injection stay the same; see explicit artifact registration.
Topics
Section titled “Topics”- Getting Started — define and register your first reactor
- Subscribe to External Event Stores
- Event Processing — choose a handler signature and supply its dependencies
- Filtering — restrict delivery by event metadata
- Fluent Registration — register typed .NET delegates without a reactor class
- Runtime Registration — handle event types known only by identifier in .NET
- Returning Side Effects
- Handling Events in Batches
- Event Sequence
- Once-Only Processing
- Start at the Tail
- Replay
- Delivery Identity
- Filtering by appended event metadata
- Tagging Reactors
Reactors react to events. To react instead to a read model being added, modified or removed — with the same convention-based methods, dependencies and side effects — see Reacting to Read Model Changes.