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 DocumentStore 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));
}