johanhaleby / occurrent

Unintrusive Event Sourcing Library for the JVM
https://occurrent.org
124 stars 16 forks source link

Add support for starting catchup subscriptions in the background when using @Subscription annotation #164

Closed johanhaleby closed 1 month ago

johanhaleby commented 5 months ago

For example, if you have many events or a subscription whose processing is very slow:

@Subscription(id = "LogWhenGuessTooSmall",startAt = BEGINNING_OF_TIME)
void logWhenGuessTooSmall(PlayerGuessedANumberThatWasTooSmall e) {
    log.info("Player {} guessed a too small number in game {}", e.playerId(), e.gameId());
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        throw new RuntimeException(ex);
    }
}

then the Spring application won't start until all events have been consumed. This is not good! Instead, start the subscription in another (virtual?) thread. Should this be configurable in the annotation?

johanhaleby commented 5 months ago

Maybe by setting a startUpMode = BACKGROUND or startUpMode = FOREGROUND. If the latter, call waitUntilStarted on the subscription

johanhaleby commented 5 months ago

The problem is that the CatchupSubscriptionModel always does "catch-up" when calling subscribe. Instead it should start in the background, and only "synchronize" if waitUntilStarted is called.