At which point/part of code does Axon apply previous events to give me current state of my object?

I am learning Axon, and seem to be making good progress. I created my application following the Bike Rental example. However, I am not sure as to the section of code that replays/applies previous events to my plain POJO and gives me the current state.

@EventHandler
public void on(BookReturnedEvnt event) {
    this.bookInforRepository
            .findById(event.getId())
            .map(bk -> {
                bk.setStatus(event.isStatus());
                return bk;
            });
}

@QueryHandler(queryName = "findOne")
public BookInfo findOne(String id){
    return this.bookInforRepository.findById(id).orElse(null);
}

Hi Nation,

Not sure if this answers your question (probably in reverse :slight_smile: ) but here goes:

When an Axon Query Application starts, by default it makes use of a TrackingEventProcessor, which unlike the SubscribedEventProcessor, is managed from the Query Applications Thread to access the events source, rather than the Event Publisher’s thread. To have the Application start with a clean slate, ignoring all events made prior to its startup, a custom configuration TrackingEventProcessorConfiguration is defined in a @Configuration class, which sets .andInitialTrackingToken(StreamableMessageSource::createHeadToken); (See AxonConfiguration.class)"

I have an example of this at: https://github.com/grechjoseph/axon-distributed/blob/7-ignore-event-made-prior-to-startup/axon-query-one/src/main/java/com/jg/axonqueryone/config/AxonConfiguration.java (The branch related to this is the 7.-ignore-events-made-prior-to-startup branch.

I am not a professional AxonIQ developer, however I enjoy playing around with it. Feel free to utilize the whole example project and I really hope it helps.

Hi Nation,

Not sure if this answers your question (probably in reverse :slight_smile: ) but here goes:

When an Axon Query Application starts, by default it makes use of a TrackingEventProcessor, which unlike the SubscribedEventProcessor, is managed from the Query Applications Thread to access the events source, rather than the Event Publisher’s thread. To have the Application start with a clean slate, ignoring all events made prior to its startup, a custom configuration TrackingEventProcessorConfiguration is defined in a @Configuration class, which sets .andInitialTrackingToken(StreamableMessageSource::createHeadToken); (See AxonConfiguration.class)"

I have an example of this at: https://github.com/grechjoseph/axon-distributed/blob/7-ignore-event-made-prior-to-startup/axon-query-one/src/main/java/com/jg/axonqueryone/config/AxonConfiguration.java (The branch related to this is the 7.-ignore-events-made-prior-to-startup branch.

I am not a professional AxonIQ developer, however I enjoy playing around with it. Feel free to utilize the whole example project and I really hope it helps.

Hi Joseph,

Thank you for your response and the provided link. If I got your right, I do not intend to have the application start on a new slate. The question is in that when I request an Object/Entity by it’s Id, at which part of the code does Axon start to build this Entity from previous events to the current state? Does it do that when I start the application? Example: If I borrow a book like so: Borrow event, Return event, Borrow event and Return event. Where do I log in my code so that when I request my entity by its id, I see these events applied to the last one like so:Borrow event, Return event, Borrow event and Return event.?

Aha then ignore my first answer for this case.

To answer your question, on startup of an application where the @EventHandlers exist will replay the events (you may verify this using breakpoints at startup), this is because it is at this point that the TrackingEventProcessor is initialized and queries the Event Store for any non-processed (or processed but lost track due to restart, which is where my first answer may apply) events.

If I may elaborate a bit more: The way @EventHandlers are triggers is one of two ways. Either Subscribing Event Processor, which receives new events when they are published, executed from the publisher’s thread (command side), or (by default) TrackingEventProcessor, to which the above applies.

Maybe this link may bring more insight as well https://blog.codecentric.de/en/2019/12/axon-replaying-made-easy-with-endpoints/ . Hope I’ve been of help!

Yes, now I am on track! Thank you, now that answered my question. Now it looks like I need to look into the Subscribing Event Processor and TrackingEventProcessor APIs. Thanks for the linked article. BTW is there a way/API I can get a list of these events through a REST end point, I want to see them as history?

This might have the answer you re looking for https://groups.google.com/forum/#!topic/axonframework/ec4TFliF77M.

Yes, exactly I just bumped into that post too like recently and implemented mine like so:

Thanks for the help, I appreciate it so much!

@RequestMapping(value = "/history/{id}", method = RequestMethod.GET)
public List<String> getAllEvents(@PathVariable("id") String id) {
    return this.eventStore.readEvents(id)
            .asStream()
            .map(event ->{
                System.out.println("Event : "+event);
                return event.toString();})
            .collect(Collectors.toList());
}

You’re very welcome, and good luck with using Axon.