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

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

Example

CleanCloseable subscription = store.changes()
    .forDocument("employees/1")
    .subscribe(Observers.create(change -> {
        switch (change.getType()) {
            case PUT:
                // do something
                break;
            case DELETE:
                // do something
                break;
        }
    }));

forDocumentsInCollection

To observe all document changes in particular collection use forDocumentInCollection method. This method filters documents by @collection metadata property value.

Syntax

IChangesObservable<DocumentChange> forDocumentsInCollection(String collectionName);

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

Information

Overload with TEntity type uses conventions.GetCollectionName to get collection name.

Example

CleanCloseable subscription = store
    .changes()
    .forDocumentsInCollection(Employee.class)
    .subscribe(Observers.create(change -> {
        System.out.println(change.getType() + " on document " + change.getId());
    }));

or

String collectionName = store.getConventions().getFindCollectionName().apply(Employee.class);
CleanCloseable subscription = store
    .changes()
    .forDocumentsInCollection(collectionName)
    .subscribe(Observers.create(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

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

Example

CleanCloseable subscription = store
    .changes()
    .forDocumentsStartingWith("employees/1") // employees/1, employees/10, employees/11, etc.
    .subscribe(Observers.create(change -> {
        System.out.println(change.getType() + " on document " + change.getId());
    }));

forAllDocuments

To observe all document changes use forAllDocuments method.

Syntax

IChangesObservable<DocumentChange> forAllDocuments();
Return Value
IChangesObservable<DocumentChange> Observable that allows to add subscriptions to notifications for all documents.

Example

CleanCloseable subscription = store
    .changes()
    .forAllDocuments()
    .subscribe(Observers.create(change -> {
        System.out.println(change.getType() + " on document " + change.getId());
    }));

DocumentChange

Name Type Description
Type DocumentChangeTypes Document change type enum
Id String Document identifier
CollectionName String Document's collection name
TypeName String Type name
ChangeVector String Document's ChangeVector

DocumentChangeTypes

Name
NONE
PUT
DELETE
BULK_INSERT_STARTED
BULK_INSERT_ENDED
BULK_INSERT_ERROR
DELETE_ON_TOMBSTONE_REPLICATION
CONFLICT
COMMON