Unwatch/re-watch change stream without restarting the application

How to stop a mongodb changestream temporarily and resume it again using ReactiveMongoTemplate ?

public Flux<Example> watch() {
  final ChangeStreamOptions changeStreamOptions = ChangeStreamOptions.builder().returnFullDocumentOnUpdate().build();
  return reactiveMongoTemplate.changeStream("collection", changeStreamOptions, Example.class)
          .filter(e -> e.getOperationType() != null)
          .mapNotNull(ChangeStreamEvent::getBody);
}

I’m trying to create a rest endpoint that should be able to stop the changestream for sometime while we do some database maintenance and then invoke the endpoint again to resume the stream from where it left off using resumeAt(timestamp)

Change stream can be unsubscribed/stopped by disposing the subscription

Disposable subscription = service.watch()
       .subscribe(exampleService::doSomething)

// cancel the subscription       
subscription.dispose();

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.