Search code examples
axon

Axoniq Event Handler Resuming from offset


I am looking at the AxonIQ framework and have managed to get a test application up and running. But I have a question about how EventHandlers should be treated when using a store that has persistence in the Read Model.

From my (possible naive) understanding. @EventHandler annotated methods in my Projection class get called from the beginning when first launched. This would mechanism seems to assume that the Projection utilises some kind of in volatile store (e.g. an in memory sql like h2) which is re-created from scratch during the application bootup.

However, if the store was persistent in something like Elastic Search, I would want the @EventHandler to resume from its last persisted event instead of from the beginning event.

Is there anyway to control the behaviour of the @EventHandler in this way?


Solution

  • Axon has two types of Event Processors: Subscribing and Tracking.

    The Subscribing mode (which was the default up to Axon 3) will handle events in the thread that delivers them. That means you're at "the mercy" of the delivery guarantees of whichever component delivers the events.

    The Tracking mode (which is the default since Axon 4 when using an Event Store or otherwise a source that supports it) will have events handled in dedicated threads, managed by the Event Processor itself. That means events are handled asynchronously from the actual publication mechanism.

    The Tracking Event Processor uses Tokens to keep track of progress. These Tokens are stored in a TokenStore and updates as the Processor has correctly processed each incoming event (possibly batched). You decide where those tokens are stored. If you update a relational database, we recommend storing the tokens in the same database, so that event changes and tokens are updated atomically.

    If you don't specify any TokenStore, it depends on whether you're on Spring Boot, in which case Axon will attempt to detect a suitable TokenStore implementation for you. Otherwise, it may very well just be an in-memory TokenStore, which causes Processors to re-initialize on every startup (and possibly start from the beginning).

    To configure a TokenStore

    • On Spring (Boot), simply add a bean of type TokenStore with the implementation you want to use
    • When using Axon's Configuration API, on the EventProcessingConfigurer, use one of the registerTokenStore(...) methods.

    When the Tracking Processor starts, it will check the Token Store for previous progress, and continue from there automatically.