Hi,
Scenario:
I have 2 spring-boot services acting as command and query as below.
- Command-Service: Responsible for handling commands, events, maintaining aggregate’s state and storing data into database.
- Query-Service: Responsible for handling queries in order to fetch data from databse.
Please note that both services are sharing the same instance of axon-server and also the same databse(in this case ElsaticSearch).
My UI is designed in such a way that when user submits the form to create data, an ajax call happens which in turn performs 2 rest calls in below order:
- First call is a post request which goes to Command-Service which performs below:
- Creates a CreateDataCommand object with formdata and fires the same as below: org.axonframework.commandhandling.gateway.CommandGateway.sendAndWait( CreateDataCommand).
- The CreateDataCommand is handled in the aggregate which fires DataCreatedEvent:
@CommandHandler
private DataAggregate(CreateDataCommand createDataCommand ) {
AggregateLifecycle.apply(new DataCreatedEvent ());
} - DataCreatedEvent is handled at 2 places,
- In the Aggraget itself, where the Aggregate updates its state:
@EventSourcingHandler
private void on(AppCreatedEvent appCreatedEvent) {
// updated aggregate’s state
} - In the Projection, Which saves data into the database.
@EventHandler
private void on(AppCreatedEvent appCreatedEvent) {
// Saves data to database.
}
- In the Aggraget itself, where the Aggregate updates its state:
- Upon sucessful execution of above the ajax immediately sends a get request to Query-Service which performs below:
- Fires GetDataQuery query as below: org.axonframework.queryhandling.QueryGateway.query(GetDataQuery, ResponseType).
- The GetDataQuery is handled to fetch data from database as below:
@QueryHandler
List getData(GetDataQuery getDataQuery){
// hits the databse and returns data.
}
Problem:
Even though the data is being saved into the database by Command-Service, the call to Query-Service is triggred and it always returns empty list. On second call onwards it returns the correct data. This is happening because Query-Service hits the database before Command-Service finishes*.*
While going through some posts on the forum i learnt that axon never wait for an event being handled when sending a command. The responsibility of the command stops at publishing the results of that command (i.e. events).
Can someone help me in achieving the full consistency or find a way to eradicate the side effects that are occurring on account of above phenomenon.