Class CatchupSubscriptionModel

java.lang.Object
org.occurrent.subscription.blocking.durable.catchup.CatchupSubscriptionModel
All Implemented Interfaces:
DelegatingSubscriptionModel, Subscribable, SubscriptionModel, SubscriptionModelLifeCycle

public class CatchupSubscriptionModel extends Object implements SubscriptionModel, DelegatingSubscriptionModel
A SubscriptionModel that can read historic cloud events from the all event streams (see EventStoreQueries.all()) until caught up with the PositionAwareSubscriptionModel.globalSubscriptionPosition() of the subscription (you probably want to narrow the historic set events of events by using a Filter when subscribing). It'll automatically switch over to the wrapped subscription model when all history events are read and the subscription has caught-up.
Important: The subscription model will only stream historic events if started with a TimeBasedSubscriptionPosition, by default (i.e. if StartAt.subscriptionModelDefault() is used), it'll NOT replay historic events, but instead delegate to the wrapped subscription model. Thus, to start the CatchupSubscriptionModel and make it replay historic events you can start it like this:
 var subscriptionModel = new CatchupSubscriptionModel(..);
 // All examples below are equivalent:
 subscriptionModel.subscribeFromBeginningOfTime("subscriptionId", e -> System.out.println("Event: " + e);
 subscriptionModel.subscribe("subscriptionId", StartAtTime.beginningOfTime(), e -> System.out.println("Event: " + e);
 subscriptionModel.subscribe("subscriptionId", StartAt.subscriptionPosition(TimeBasedSubscription.beginningOfTime()), e -> System.out.println("Event: " + e);
 

If you're using Kotlin you can import the extension functions from org.occurrent.subscription.blocking.durable.catchup.CatchupSubscriptionModelExtensions.kt and do:

 subscriptionModel.subscribe("subscriptionId", StartAt.beginningOfTime()) { e ->
      println("Event: $e")
 }
 

Note that the implementation uses an in-memory cache (default size is 100 but this can be configured using a CatchupSubscriptionModelConfig) to reduce the number of duplicate event when switching from historic events to the current cloud event position. It's highly recommended that the application logic is idempotent if the cache size doesn't cover all duplicate events.


Also note that the if a the subscription crashes during catch-up mode it'll continue where it left-off on restart, given the no specific `StartAt` position is supplied (i.e. if StartAt.subscriptionModelDefault() is used). For this to work, the subscription must store the subscription position in a SubscriptionPositionStorage implementation periodically. It's possible to configure how often this should happen in the CatchupSubscriptionModelConfig.