Class CatchupSubscriptionModel
- All Implemented Interfaces:
DelegatingSubscriptionModel,Subscribable,SubscriptionModel,SubscriptionModelLifeCycle
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.
-
Constructor Summary
ConstructorsConstructorDescriptionCatchupSubscriptionModel(PositionAwareSubscriptionModel subscriptionModel, EventStoreQueries eventStoreQueries) Create a new instance ofCatchupSubscriptionModelthe uses a defaultCatchupSubscriptionModelConfigwith a cache size of 100 but store the subscription position during the catch-up phase (i.e.CatchupSubscriptionModel(PositionAwareSubscriptionModel subscriptionModel, EventStoreQueries eventStoreQueries, CatchupSubscriptionModelConfig config) Create a new instance ofCatchupSubscriptionModelthe uses the suppliedCatchupSubscriptionModelConfig. -
Method Summary
Modifier and TypeMethodDescriptionvoidcancelSubscription(String subscriptionId) booleanbooleanbooleanstatic booleanisTimeBasedSubscriptionPosition(org.occurrent.subscription.StartAt startAt) static booleanisTimeBasedSubscriptionPosition(org.occurrent.subscription.SubscriptionPosition subscriptionPosition) voidpauseSubscription(String subscriptionId) resumeSubscription(String subscriptionId) voidshutdown()voidstart(boolean resumeSubscriptionsAutomatically) voidstop()subscribe(String subscriptionId, org.occurrent.subscription.SubscriptionFilter filter, org.occurrent.subscription.StartAt startAt, Consumer<io.cloudevents.CloudEvent> action) subscribeFromBeginningOfTime(String subscriptionId, Consumer<io.cloudevents.CloudEvent> action) Shortcut to start subscribing to all events from begging of time.subscribeFromBeginningOfTime(String subscriptionId, org.occurrent.subscription.SubscriptionFilter filter, Consumer<io.cloudevents.CloudEvent> action) Shortcut to start subscribing to events matching the supplied filter from begging of time.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.occurrent.subscription.api.blocking.DelegatingSubscriptionModel
getDelegatedSubscriptionModelRecursivelyMethods inherited from interface org.occurrent.subscription.api.blocking.Subscribable
subscribe, subscribe, subscribeMethods inherited from interface org.occurrent.subscription.api.blocking.SubscriptionModelLifeCycle
start
-
Constructor Details
-
CatchupSubscriptionModel
public CatchupSubscriptionModel(PositionAwareSubscriptionModel subscriptionModel, EventStoreQueries eventStoreQueries) Create a new instance ofCatchupSubscriptionModelthe uses a defaultCatchupSubscriptionModelConfigwith a cache size of 100 but store the subscription position during the catch-up phase (i.e. if the application crashes or is shutdown during the catch-up phase then the subscription will start from the beginning on application restart). After the catch-up phase has completed, thePositionAwareSubscriptionModelwill dictate how often the subscription position is stored.- Parameters:
subscriptionModel- The subscription that'll be used to subscribe to new events after catch-up is completed.eventStoreQueries- The API that will be used for catch-up
-
CatchupSubscriptionModel
public CatchupSubscriptionModel(PositionAwareSubscriptionModel subscriptionModel, EventStoreQueries eventStoreQueries, CatchupSubscriptionModelConfig config) Create a new instance ofCatchupSubscriptionModelthe uses the suppliedCatchupSubscriptionModelConfig. After catch-up mode has completed, thePositionAwareSubscriptionModelwill dictate how often the subscription position is stored.- Parameters:
subscriptionModel- The subscription that'll be used to subscribe to new events after catch-up is completed.eventStoreQueries- The API that will be used for catch-upconfig- The configuration to use
-
-
Method Details
-
subscribeFromBeginningOfTime
public Subscription subscribeFromBeginningOfTime(String subscriptionId, org.occurrent.subscription.SubscriptionFilter filter, Consumer<io.cloudevents.CloudEvent> action) Shortcut to start subscribing to events matching the supplied filter from begging of time. Same as doing:subscriptionModel.subscribe(<subscriptionId>, <filter>, StartAtTime.beginningOfTime(), <action>);
-
subscribeFromBeginningOfTime
public Subscription subscribeFromBeginningOfTime(String subscriptionId, Consumer<io.cloudevents.CloudEvent> action) Shortcut to start subscribing to all events from begging of time. Same as doing:subscriptionModel.subscribe(<subscriptionId>, StartAtTime.beginningOfTime(), <action>);
-
subscribe
public Subscription subscribe(String subscriptionId, org.occurrent.subscription.SubscriptionFilter filter, org.occurrent.subscription.StartAt startAt, Consumer<io.cloudevents.CloudEvent> action) - Specified by:
subscribein interfaceSubscribable
-
stop
public void stop()- Specified by:
stopin interfaceSubscriptionModelLifeCycle
-
start
public void start(boolean resumeSubscriptionsAutomatically) - Specified by:
startin interfaceSubscriptionModelLifeCycle
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceSubscriptionModelLifeCycle
-
isRunning
- Specified by:
isRunningin interfaceSubscriptionModelLifeCycle
-
isPaused
- Specified by:
isPausedin interfaceSubscriptionModelLifeCycle
-
resumeSubscription
- Specified by:
resumeSubscriptionin interfaceSubscriptionModelLifeCycle
-
pauseSubscription
- Specified by:
pauseSubscriptionin interfaceSubscriptionModelLifeCycle
-
cancelSubscription
- Specified by:
cancelSubscriptionin interfaceSubscriptionModelLifeCycle
-
shutdown
@PreDestroy public void shutdown()- Specified by:
shutdownin interfaceSubscriptionModelLifeCycle
-
isTimeBasedSubscriptionPosition
public static boolean isTimeBasedSubscriptionPosition(org.occurrent.subscription.StartAt startAt) -
isTimeBasedSubscriptionPosition
public static boolean isTimeBasedSubscriptionPosition(org.occurrent.subscription.SubscriptionPosition subscriptionPosition) -
getDelegatedSubscriptionModel
- Specified by:
getDelegatedSubscriptionModelin interfaceDelegatingSubscriptionModel
-