@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.
Basic Usage
Annotate a class with@ChangeStream and add at least one event handler method (@OnChange, @OnInsert, @OnUpdate, @OnDelete, @OnReplace):
@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.Attributes Reference
Identification
MongoDB Target
Document Options
FullDocumentMode values
FullDocumentBeforeChangeMode values
Deployment
DeploymentMode values
Behaviour
Collection Resolution
Ifcollection is not explicitly set, FlowWarden resolves it from documentType:
- If
documentTypehas 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, startup fails with an explicit error.
Name Resolution
The stream name is resolved in this order:name()attribute if non-emptyvalue()attribute if non-empty- Kebab-case of the class simple name (e.g.
OrderStreamHandler→order-stream-handler)
Handler Methods
A@ChangeStream class must declare at least one handler method. FlowWarden supports two styles:
Generic: @OnChange
Catch-all for every operation that is not covered by a typed handler in the same class, including DROP and INVALIDATE. The annotation takes no attributes. Only one @OnChange method is allowed per class.
Typed: @OnInsert, @OnUpdate, @OnDelete, @OnReplace
Route events to specific methods by operation type. At most one method per typed annotation.
Combining Both
When both typed handlers and@OnChange are present, typed handlers take priority. @OnChange acts as a fallback for operation types without a dedicated handler.
Supported Signatures
Typed handler methods (@OnInsert, @OnUpdate, @OnDelete, @OnReplace) support three signature styles:
@OnChange only supports the CONTEXT_ONLY style: void handle(ChangeStreamContext ctx).
Examples
Minimal
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.With Explicit Name and Collection
With Pre-Image (Before/After Comparison)
Disabled Stream
Custom MongoTemplate
When your application uses multiple MongoDB connections, specify which template to use:Programmatic Start/Stop
Streams can be controlled at runtime via theFlowWardenStreamManager interface:
Best Practices
- One handler class per collection — keep stream handlers focused and cohesive.
- Set
documentTypewhen you need typed access — otherwise documents arrive asorg.bson.Document. - Use
enabled = falseto temporarily disable a stream without removing the code. - Set
autoStart = falseif you need to start the stream programmatically after some initialization logic.
See Also
Event Handlers
@OnInsert, @OnUpdate, @OnDelete, @OnChange — handler method signatures in detail.@Filter
Push server-side predicates to MongoDB’s aggregation pipeline.
@Checkpoint
Configure resume token persistence for crash-resilient streams.
Configuration
flowwarden.default-mode and other application properties.