0
votes

I'm not sure how to properly ask this question but here it is:

I'm starting the saga on specific event, then im dispatching the command which is supposed to create some aggregate and then send another event which will be handled by the saga to proceed with the logic.

However each time i'm restarting the application i get an error saying that event for aggregate at sequence x was already inserted, which, i suppose is because the saga has not yet been finished and when im restarting it it starts it again by trying to create new aggregate.

Question is, is there any way in the axoniq to track progress of the saga? Like should i set some flags when i receive event and wrap in ifs the aggregate creation? Maybe there is another way which i'm not seeing, i just dont want the saga to be replayed from the start.

Thanks

2
Tiny nit, but I'd rename the title of your question to 'Axon - creating Aggregate inside a Saga'. AxonIQ is the company name, but not the product name. ;)Steven

2 Answers

4
votes

The solution you've posted definitely would work. Let me explain the scenario you've hit here though, for other peoples reference too.

In an Axon Framework 4.x application, any Event Handling Component, thus also your Saga instances, are backed by a TrackingEventProcessor. The Tracking Event Processor "keeps track of" which point in the Event Stream it is handling events. It stores this information through a TrackingToken, for which the TokenStore is the delegating piece of work.

If you haven't specified a TokenStore however, you will have in-memory TrackingTokens for every Tracking Event Processor. This means that on a restart, your Tracking Event Processor thinks "ow, I haven't done any event handling yet, let me start from the beginning of time". Due to this, your Saga instances will start a new, every time, trying to recreate the given Aggregate instance.

Henceforth, specifying the TokenStore as you did resolved the problem you had.

Note, that in a Spring Boor environment, with for example the Spring Data starter present, Axon will automatically create the JpaTokenStore for you.

0
votes

I've solved my issue by simply adding token store configuration, it does exactly what i require - track processed events.

Basic spring config:

    @Bean
    fun tokenStore(client: MongoClient): TokenStore = MongoTokenStore.builder()
        .mongoTemplate(DefaultMongoTemplate.builder().mongoDatabase(client).build())
        .serializer(JacksonSerializer.builder().build())
        .build()