Overview
@Filter performs application-side filtering in Java. All events arrive from MongoDB, and a
predicate decides which ones are forwarded to handler methods. Events that don’t pass the filter
are silently skipped — but their resume token is still checkpointed as lastSeenToken.
Unlike @Pipeline which runs a MongoDB aggregation pipeline once at
stream startup to reduce network traffic, @Filter executes on every event and can leverage
Spring beans, service calls, and any Java logic.
At most one
@Filter method is allowed per @ChangeStream class.Supported Signatures
| Return type | Parameters | Description |
|---|---|---|
Predicate<ChangeStreamContext<?>> | None | Returns a reusable predicate. Called once, predicate tested per event. |
boolean | ChangeStreamContext<?> | Direct evaluation. Called on every event. |
Basic Example
Startup Validation (Fail-Fast)
FlowWarden validates@Filter compatibility at application startup and rejects clearly
incompatible configurations immediately with a clear error message. The application will fail
to start if @Filter is combined with a typed handler covering an operation where MongoDB
does not provide a fullDocument.
Why?
@Filter predicates typically call ctx.getFullDocument(), which returns Optional.empty() for
DELETE, DROP, and INVALIDATE operations. Running the filter on a typed handler dedicated to
one of those operations is almost always a mistake, so FlowWarden rejects that combination at
startup. Combining @Filter with the @OnChange catch-all is allowed — DROP and INVALIDATE will
reach the predicate, and the predicate must handle Optional.empty() explicitly.
Rule
The following combination is rejected:@Filter + typed handler for a no-fullDocument operation
Valid Combinations
The following combinations are accepted:Quick Reference
| Handler combination | @Filter allowed? |
|---|---|
@OnInsert | Yes |
@OnUpdate | Yes |
@OnReplace | Yes |
@OnInsert + @OnUpdate | Yes |
@OnDelete | No |
@OnChange | Yes (predicate must handle Optional.empty() for DROP / INVALIDATE) |
@OnInsert + @OnDelete | No |
Checkpoint Interaction
Events rejected by@Filter are not forwarded to the handler, so they never advance
lastProcessedToken. Their resume token does, however, advance the in-memory lastSeenToken
tracker, which the heartbeat timer (@Checkpoint(saveIntervalSeconds)) persists on each tick.
This is what enables the resume cascade level-2 fallback
on streams where most events are filter-rejected.
On filter-heavy streams, a gap grows between lastSeenToken (advancing) and lastProcessedToken
(stalled until a non-filtered event succeeds). Keep saveIntervalSeconds > 0 so the heartbeat
can persist lastSeenToken regularly — otherwise the cascade has no level-2 safety net.
Combining with @Pipeline
@Filter can coexist with @Pipeline on the same @ChangeStream,
forming a double filtering funnel:
Typical use case: pre-filter operationType = insert | update and status = PAID
server-side with @Pipeline, then verify application-side with @Filter that the tenant
is active via a Spring service call.
When combining
@Pipeline + @Filter, events that pass the server-side pipeline reach
FlowWarden and advance lastSeenToken regardless of whether @Filter accepts them. The
heartbeat timer persists lastSeenToken, enabling the resume cascade
level-2 fallback on streams where lastProcessedToken lags behind.See Also
@Pipeline
Server-side aggregation pipeline filtering
Filtering Events Guide
Complete guide combining @Pipeline and @Filter
@Checkpoint
Resume token persistence and dual checkpoint
Event Handlers
@OnInsert, @OnUpdate, @OnDelete, @OnChange