Session: How to Subscribe to Events

The concept of events provides users with a mechanism to perform custom actions in response to operations taken in a session.

The listener is invoked when a particular action is executed on an entity or querying is performed.

Subscribing to an event

Subscribing an event can be done in the IDocumentStore object, which will be valid for all future sessions or subscribing in an already opened session with session.advanced() which will overwrite the existing event for the current session.

beforeStoreListener

This event is invoked as a part of saveChanges but before it is actually sent to the server.

It takes the argument BeforeStoreEventArgs that consists of the Session entity's ID and the entity itself.

Example

Say we want to discontinue all of the products that are not in stock.

private void onBeforeStoreEvent(Object sender, BeforeStoreEventArgs args) {
    if (args.getEntity() instanceof Product) {
        Product product = (Product) args.getEntity();
        if (product.unitsInStock == 0) {
            product.setDiscontinued(true);
        }
    }
}

After we subscribe to the event, every stored entity will invoke the method.

// subscribe to the event
store.addBeforeStoreListener(this::onBeforeStoreEvent);

try (IDocumentSession session = store.openSession()) {
    Product product1 = new Product();
    product1.setName("RavenDB v3.5");
    product1.setUnitsInStock(0);

    session.store(product1);

    Product product2 = new Product();
    product2.setName("RavenDB v4.0");
    product2.setUnitsInStock(1000);
    session.store(product2);

    session.saveChanges();  // Here the method is invoked
}

beforeDeleteListener

This event is invoked as a part of saveChanges, but before it actually sends the deleted entities to the server.

It takes the argument BeforeDeleteEventArgs, that consists of the Session entity's ID and the entity itself.

Example

To prevent anyone from deleting entities we can create a method as follows:

private void onBeforeDeleteEvent(Object sender, BeforeDeleteEventArgs args) {
    throw new NotImplementedException("Sample");
}

and subscribe it to the session:

// subscribe to the event
store.addBeforeDeleteListener(this::onBeforeDeleteEvent);

// open a session and delete entity
try (IDocumentSession session = store.openSession()) {
    Product product = session.load(Product.class, "products/1-A");

    session.delete(product);

    session.saveChanges(); // NotImplementedException will be thrown here
}

afterSaveChangesListener

This event is invoked after the saveChanges is returned. It takes the argument AfterSaveChangesEventArgs that consists of the Session entity's ID and the entity itself with the updated metadata from the server.

Example

If we want to log each entity that was saved, we can create a method as follows:

private void onAfterSaveChangesEvent(Object sender, AfterSaveChangesEventArgs args) {
    if (log.isLoggable(Level.INFO)) {
        log.info("Document " + args.getDocumentId() + " was saved");
    }
}

beforeQueryListener

This event is invoked just before the query is sent to the server.

It takes the argument BeforeQueryEventArgs, that consists of the Session and the IDocumentQueryCustomization.

Example I

If you want to disable caching of all query results, you can implement the method as follows:

private void onBeforeQueryEvent(Object sender, BeforeQueryEventArgs args) {
    args.getQueryCustomization().noCaching();
}

Example II

If you want each query to wait for non-stale results you can create an event as follows:

private void onBeforeQueryEvent(Object sender, BeforeQueryEventArgs args) {
    args.getQueryCustomization().waitForNonStaleResults(Duration.ofSeconds(30));
}

OnBeforeConversionToDocument

This event is invoked before conversion of an entity to blittable JSON document. E.g. it's called when sending a document to a server.

It takes the argument BeforeConversionToDocumentEventArgs, that consists of an entity, its ID and the session instance.

public void onBeforeConversionToDocumentInvoke(String id, Object entity) {
    EventHelper.invoke(onBeforeConversionToDocument, this, new BeforeConversionToDocumentEventArgs(this, id, entity));
}

OnAfterConversionToDocument

This event is invoked after conversion of an entity to blittable JSON document.

It takes the argument AfterConversionToDocumentEventArgs, that consists of an entity, its ID, the session instance and converted JSON document.

public void onAfterConversionToDocumentInvoke(String id, Object entity, Reference<ObjectNode> document) {
    if (!onAfterConversionToDocument.isEmpty()) {
        AfterConversionToDocumentEventArgs eventArgs = new AfterConversionToDocumentEventArgs(this, id, entity, document);
        EventHelper.invoke(onAfterConversionToDocument, this, eventArgs);

        if (eventArgs.getDocument().value != null && eventArgs.getDocument().value != document.value) {
            document.value = eventArgs.getDocument().value;
        }
    }
}

OnBeforeConversionToEntity

This event is invoked before conversion of a JSON document to an entity. E.g. it's called when loading a document.

It takes the argument BeforeConversionToEntityEventArgs, that consists of a JSON document, its ID and type, and the session instance.

public void onBeforeConversionToEntityInvoke(String id, Class clazz, Reference<ObjectNode> document) {
    if (!onBeforeConversionToEntity.isEmpty()) {
        BeforeConversionToEntityEventArgs eventArgs = new BeforeConversionToEntityEventArgs(this, id, clazz, document);
        EventHelper.invoke(onBeforeConversionToEntity, this, eventArgs);

        if (eventArgs.getDocument() != null && eventArgs.getDocument().value != document.value) {
            document.value = eventArgs.getDocument().value;
        }
    }
}

OnAfterConversionToEntity

This event is invoked after conversion of a JSON document to an entity.

public void onAfterConversionToEntityInvoke(String id, ObjectNode document, Object entity) {
    AfterConversionToEntityEventArgs eventArgs = new AfterConversionToEntityEventArgs(this, id, document, entity);
    EventHelper.invoke(onAfterConversionToEntity, this, eventArgs);
}

It takes the argument AfterConversionToEntityEventArgs, that consists of a JSON document, its ID, the session instance and a converted entity.