Changes API: How to subscribe to document changes?

Following methods allow you to subscribe to document changes:

ForDocument

Single document changes can be observed using forDocument method.

Syntax

public IObservable<DocumentChangeNotification> forDocument(String docId);
Parameters
docId String Id of a document for which notifications will be processed.
Return Value
IObservable<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document.

Example

Closeable subscription = store
  .changes()
  .forDocument("employees/1")
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      switch (change.getType()) {
        case PUT:
          // do something
          break;
        case DELETE:
          // do something
          break;
        default:
          break;
      }
    }
  }));

ForDocumentsInCollection

To observe all document changes in particular collection use forDocumentInCollection method. This method filters documents by Raven-Entity-Name metadata property value.

Syntax

public IObservable<DocumentChangeNotification> forDocumentsInCollection(String collectionName);

public IObservable<DocumentChangeNotification> forDocumentsInCollection(Class<?> clazz);
Parameters
collectionName String Name of document collection for which notifications will be processed.
Return Value
IObservable<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document collection name.

Information

Overload with Class uses Conventions.getTypeTagName to get collection name.

Example

Closeable subscription = store
  .changes()
  .forDocumentsInCollection(Employee.class)
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      System.out.println(change.getType() + " on document " + change.getId());
    }
  }));

or

String collectionName = store.getConventions().getTypeTagName(Employee.class);
Closeable subscription = store
  .changes()
  .forDocumentsInCollection(collectionName)
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      System.out.println(change.getType() + " on document " + change.getId());
    }
  }));

ForDocumentsOfType

To observe all document changes for given type use forDocumentsOfType method. This method filters documents by Raven-Clr-Type metadata property value.

Syntax

public IObservable<DocumentChangeNotification> forDocumentsOfType(String typeName);

public IObservable<DocumentChangeNotification> forDocumentsOfType(Class<?> clazz);
Parameters
typeName or clazz String or Class Name of class or class for which notifications will be processed. If default conventions are used then full type name without version information should be passed.
Return Value
IObservable<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document type name.

Information

Overload with Class uses Conventions.findClrTypeName to get type name.

Example

Closeable subscription = store
  .changes()
  .forDocumentsOfType(Employee.class)
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      System.out.println(change.getType() + " on document " + change.getId());
    }
  }));

or

String typeName = store.getConventions().getFindJavaClassName().find(Employee.class);
Closeable subscription = store
  .changes()
  .forDocumentsOfType(typeName)
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      System.out.println(change.getType() + " on document " + change.getId());
    }
  }));

ForDocumentsStartingWith

To observe all document changes for documents with Id that contains given prefix use forDocumentsStartingWith method.

Syntax

public IObservable<DocumentChangeNotification> forDocumentsStartingWith(String docIdPrefix);
Parameters
docIdPrefix String Document Id prefix for which notifications will be processed.
Return Value
IObservable<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for given document Id prefix.

Example

Closeable subscription = store
  .changes()
  .forDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      System.out.println(change.getType() + " on document " + change.getId());
    }
  }));

ForAllDocuments

To observe all document changes use forAllDocuments method.

Syntax

public IObservable<DocumentChangeNotification> forAllDocuments();
Return Value
IObservable<DocumentChangeNotification> Observable that allows to add subscriptions to notifications for all documents.

Example

Closeable subscription = store
  .changes()
  .forAllDocuments() // employees/1, orders/1, customers/1, etc.
  .subscribe(Observers.create(new Action1<DocumentChangeNotification>() {
    @Override
    public void apply(DocumentChangeNotification change) {
      System.out.println(change.getType() + " on document " + change.getId());
    }
  }));