Cratis Chronicle reactors: choose an event sequence
A Cratis Chronicle reactor observes one event sequence: the ordered source of events it handles. Select the sequence that contains the events your handler needs. Without another selection, reactors read the default event log, although .NET can infer an inbox for events from another event store.
In .NET, [EventSequence] and the eventSequence parameter on [Reactor] make the selection explicit. If both are present, [EventSequence] takes precedence. An explicit selection also prevents automatic inbox routing.
Using the [EventSequence] Attribute
Section titled “Using the [EventSequence] Attribute”Apply [EventSequence] to a .NET reactor class when the sequence is the only option you need to set:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Reactors;
[EventType]public record EventSequenceShipmentDispatched(string TrackingNumber);
[EventSequence("fulfillment-events")]public class EventSequenceShipmentReactor : IReactor{ public Task ShipmentDispatched(EventSequenceShipmentDispatched @event, EventContext context) => NotifyCarrierAsync(@event.TrackingNumber);
Task NotifyCarrierAsync(string trackingNumber) => Task.CompletedTask;}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.EventSequenceimport io.cratis.chronicle.observation.Reactor
@EventTypedata class ReactorsEventSequenceOrderDelivered(val orderId: String)
@Reactor@EventSequence("outbox")class ReactorsEventSequenceDeliveryNotifications { fun delivered(event: ReactorsEventSequenceOrderDelivered) { // Observed from the "outbox" sequence, without giving up the conventional identifier. }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.EventSequence;import io.cratis.chronicle.observation.Reactor;
@EventTyperecord ReactorsEventSequenceOrderDelivered(String orderId) {}
@Reactor@EventSequence("outbox")class ReactorsEventSequenceDeliveryNotifications { void delivered(ReactorsEventSequenceOrderDelivered event) { // Observed from the "outbox" sequence, without giving up the conventional identifier. }}Elixir does not support this workflow yet.import { EventContext, eventType, onceOnly, reactor } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class EventSequenceReactorShipmentDispatched { @field(String) readonly trackingNumber: string;
constructor(trackingNumber: string) { this.trackingNumber = trackingNumber; }}
@reactor('', 'fulfillment-events')class EventSequenceShipmentReactor { @onceOnly() async eventSequenceReactorShipmentDispatched(event: EventSequenceReactorShipmentDispatched, context: EventContext): Promise<void> { await this.notifyCarrier(event.trackingNumber); }
private async notifyCarrier(trackingNumber: string): Promise<void> {}}Kotlin and Java also provide @EventSequence, which takes precedence over the eventSequence parameter on @Reactor. TypeScript uses eventSequenceId on its reactor() decorator, shown under Using the [Reactor] Attribute. Elixir’s reactor declaration has no option for a non-default event sequence.
Using the [Reactor] Attribute
Section titled “Using the [Reactor] Attribute”If you already use [Reactor] to set a stable identifier, set eventSequence there too rather than adding another attribute:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Reactors;
[EventType]public record EventSequenceReactorAttributeShipmentDispatched(string TrackingNumber);
[Reactor(id: "shipment-reactor", eventSequence: "fulfillment-events")]public class EventSequenceReactorAttributeShipmentReactor : IReactor{ public Task ShipmentDispatched(EventSequenceReactorAttributeShipmentDispatched @event, EventContext context) => NotifyCarrierAsync(@event.TrackingNumber);
Task NotifyCarrierAsync(string trackingNumber) => Task.CompletedTask;}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.observation.Reactor
@EventTypedata class ReactorsEventSequenceOrderShipped(val orderId: String)
@Reactor(id = "shipping-notifications", eventSequence = "outbox")class ReactorsEventSequenceShippingNotifications { fun shipped(event: ReactorsEventSequenceOrderShipped) { // Observed from the "outbox" sequence rather than the default event log. }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.observation.Reactor;
@EventTyperecord ReactorsEventSequenceOrderShipped(String orderId) {}
@Reactor(id = "shipping-notifications", eventSequence = "outbox")class ReactorsEventSequenceShippingNotifications { void shipped(ReactorsEventSequenceOrderShipped event) { // Observed from the "outbox" sequence rather than the default event log. }}Elixir does not support this workflow yet.import { eventType, onceOnly, reactor, EventContext } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class EventSequenceReactorAttributeShipmentDispatched { @field(String) readonly trackingNumber: string;
constructor(trackingNumber: string) { this.trackingNumber = trackingNumber; }}
@reactor('shipment-reactor', 'fulfillment-events')class EventSequenceReactorAttributeShipmentReactor { // Method name must be the exact camelCase of the event's class name - // Chronicle discovers handlers by name, not by parameter type. @onceOnly() async eventSequenceReactorAttributeShipmentDispatched(event: EventSequenceReactorAttributeShipmentDispatched, context: EventContext): Promise<void> { await this.notifyCarrier(event.trackingNumber); }
private async notifyCarrier(trackingNumber: string): Promise<void> {}}In .NET, either declaration selects the same sequence when used on its own. Keep the selection in one place so a later edit does not leave conflicting values on the class.
Convenience: [EventLog]
Section titled “Convenience: [EventLog]”Use [EventLog] in .NET to select the default event log explicitly, even when handled event types carry an [EventStore] attribute pointing elsewhere:
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;using Cratis.Chronicle.Reactors;
[EventType]public record EventSequenceLogOrderPlaced(Guid OrderId);
[EventLog]public class EventSequenceLocalAuditReactor : IReactor{ public Task OrderPlaced(EventSequenceLogOrderPlaced @event, EventContext context) => WriteAuditAsync(@event.OrderId, context.Occurred);
Task WriteAuditAsync(Guid orderId, DateTimeOffset occurred) => Task.CompletedTask;}Kotlin does not support this workflow yet.Java does not support this workflow yet.Elixir does not support this workflow yet.import { EventContext, eventType, Guid, onceOnly, reactor } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class EventSequenceLogReactorOrderPlaced { @field(Guid) readonly orderId: Guid;
constructor(orderId: Guid) { this.orderId = orderId; }}
// No eventSequenceId given - observes the default event log@reactor()class EventSequenceLocalAuditReactor { @onceOnly() async eventSequenceLogReactorOrderPlaced(event: EventSequenceLogReactorOrderPlaced, context: EventContext): Promise<void> { await this.writeAudit(event.orderId, context.occurred); }
private async writeAudit(orderId: Guid, occurred: Date): Promise<void> {}}This .NET convenience prevents the handled events’ store metadata from routing the reactor to an inbox. Use it only when those events are meant to be read from the local event log.
External Event Store Inbox Routing
Section titled “External Event Store Inbox Routing”If the events originate in another event store, choosing a local sequence is only part of the setup. Follow Subscribe Reactors to External Event Stores to configure outbox-to-inbox delivery, automatic inbox routing, and observer-level [EventStore] usage.
When to Use [EventSequence] Explicitly
Section titled “When to Use [EventSequence] Explicitly”Set the sequence explicitly when the reactor reads a named sequence other than the default, or when you need to override .NET’s inferred inbox. An event sequence is not an event stream type: to select events by their source or stream metadata within the sequence, use reactor filtering.