Chapter 24. Commit log extension

Table of Contents

Description
Including in a project
Usage

Description

The goal of this module is to capture commit changes and present them to interested parties in an easy-to-process format.

Including in a project

Maven

<dependency>
    <groupId>org.apache.cayenne</groupId>
    <artifactId>cayenne-commitlog</artifactId>
    <version>4.0.B2</version>
</dependency>

Gradle

compile 'org.apache.cayenne:cayenne-commitlog:4.0.B2'

Usage

In order to use commitlog module you need to perform three steps:

  1. Mark all entities which changes you are interested in with @org.apache.cayenne.commitlog.CommitLog annotation

    @CommitLog(ignoredProperties = {"somePrivatePropertyToSkip"})
    public class MyEntity extends _MyEntity {
        // ...
    }
  2. Implement CommitLogListener interface.

    public class MyCommitLogListener implements CommitLogListener {
        @Override
        public void onPostCommit(ObjectContext originatingContext, ChangeMap changes) {
            // ChangeMap will contain all information about changes happened in performed commit
            // this particular example will print IDs of all inserted objects
            changes.getUniqueChanges().stream()
                .filter(change -> change.getType() == ObjectChangeType.INSERT)
                .map(ObjectChange::getPostCommitId)
                .forEach(id -> System.out.println("Inserted new entity with id: " + id));
        }
    }

  3. Inject your listener into ServerRuntime

    ServerRuntime.builder()
            .addModule(CommitLogModule.extend()
                    .addListener(MyCommitLogListener.class)
                    .module())