I'm a student who is quite new to the Axon framework for event-sourcing, so bear with me please. I'm currently having issues that when my QueryHandler needs access to events that have been created before the spring application was started, it won't include them.
So for example, the following event gets created:
- Webshop created
(The event creates a new Webshop and sets it balance to 100). That event gets persisted to my database. When I query the current webshop balance, I can return that the current balance is 100. (Code for event and query is shown below)
If I restart the application however and I try to query the current balance once again, previous events don't get replayed, so I can't get the current balance (after restart the events are still there in the database, so I think it is just a problem with the framework not retrieving all the previous events).
I currently use the default configuration, Axon 4.0.3 for Spring and a H2 database that stores the events to a file locally (H2 isn't the best choice, I know, but this is all for a small proof of concept showing Event Sourcing and CQRS in action, so no need for big and scalable).
@Service
@NoArgsConstructor
public class WebshopsEventHandler {
private final Map<String, Webshop> webshops = new HashMap<>();
// ... some other unrelated code
@EventSourcingHandler
public void on(ShopCreatedEvent shopCreatedEvent) {
this.webshops.put(
shopCreatedEvent.getId(),
new Webshop(
shopCreatedEvent.getId(),
shopCreatedEvent.getName(),
shopCreatedEvent.getBalance() // Balance is 100 as default
)
);
}
// ... some more unrelated code
@QueryHandler
protected Optional handle(GetCurrentBalanceQuery getCurrentBalanceQuery) {
System.out.println("Balance query");
if (webshops.containsKey(getCurrentBalanceQuery.getShopId())) {
return Optional.of(webshops.get(getCurrentBalanceQuery.getShopId()).getBalance());
} else {
return Optional.empty();
}
}
}
How can I configure Axon 4 to retrieve all the previous events after a restart?