Skip to main content
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.

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

Pre-images (fullDocumentBeforeChange) require MongoDB 6.0+ and that pre-images are explicitly enabled on the target collection:
See the MongoDB documentation for details. Without this, WHEN_AVAILABLE returns empty and REQUIRED throws an error.

Deployment

DeploymentMode values

Behaviour

Collection Resolution

If collection is not explicitly set, FlowWarden resolves it from documentType:
  1. If documentType has a Spring Data @Document(collection = "...") annotation, that value is used.
  2. Otherwise, the decapitalized simple class name is used (e.g. Order → order).
  3. If neither works, startup fails with an explicit error.

Name Resolution

The stream name is resolved in this order:
  1. name() attribute if non-empty
  2. value() attribute if non-empty
  3. 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.

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).
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.

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 the FlowWardenStreamManager interface:

Best Practices

  • 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.

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.