Declare a MongoDB Change Stream handler with a single annotation.
The @ChangeStream annotation is the foundation of the FlowWarden programming model. It turns any class into a Spring-managed Change Stream handler that watches a MongoDB collection for real-time data changes.
@ChangeStream is meta-annotated with @Component — there is no need to add @Component or @Service separately. The class is automatically registered as a Spring bean.
If collection is not explicitly set, FlowWarden resolves it from documentType:
If documentType has a Spring Data @Document(collection = "...") annotation, that value is used.
Otherwise, the decapitalized simple class name is used (e.g. Order → order).
If neither works and watchDatabase/watchDeployment is false, startup fails with an error.
// Explicit collection@ChangeStream(collection = "orders")public class OrderHandler { ... }// Inferred from @Document annotation on Order class@ChangeStream(documentType = Order.class)public class OrderHandler { ... }
Kebab-case of the class simple name (e.g. OrderStreamHandler → order-stream-handler)
The stream name must be unique across all @ChangeStream classes in the application.
Always set a meaningful name — it appears in logs, metrics, and checkpoint keys. If omitted, FlowWarden generates one from the class name, which may break checkpoints if you rename the class.
When both typed handlers and @OnChange are present, typed handlers take priority. @OnChange acts as a fallback for operation types without a dedicated handler.
Typed handler methods (@OnInsert, @OnUpdate, @OnDelete, @OnReplace) support three signature styles:
Signature
Style
Description
void handle(ChangeStreamContext<T> ctx)
CONTEXT_ONLY
Access context; get the document from ctx.
void handle(T doc)
DOCUMENT_ONLY
Receive the deserialized document directly.
void handle(T doc, ChangeStreamContext<T> ctx)
DOCUMENT_AND_CONTEXT
Both the document and the context.
@OnChange only supports the CONTEXT_ONLY style: void handle(ChangeStreamContext ctx).
If you use DOCUMENT_ONLY or DOCUMENT_AND_CONTEXT signatures, you must set a concrete documentType on @ChangeStream (not Document.class). Otherwise, startup fails with a validation error.
The handler class code is identical in both modes. The execution mode is determined globally by the flowwarden.default-mode property (IMPERATIVE or REACTIVE), which selects the appropriate stream manager at auto-configuration time.
One handler class per collection — keep stream handlers focused and cohesive.
Set documentType when you need typed access — otherwise documents arrive as org.bson.Document.
Use enabled = false to temporarily disable a stream without removing the code.
Set autoStart = false if you need to start the stream programmatically after some initialization logic.
MongoDB must be configured as a Replica Set for Change Streams to work. This applies to production and development environments. Testcontainers automatically provisions a single-node Replica Set for tests.