How to make command to wait until all events triggered against it are completed successfully

Hi Team,

I have came across a requirement where i want axon to wait untill all events in the eventbus fired against a particular Command finishes their execution. I will the brief the scenario:

I have a RestController which fires below command to create an application entity:

@RestController
class myController{
@PostMapping("/create")
@ResponseBody
public String create(
org.axonframework.commandhandling.gateway.CommandGateway.sendAndWait(new CreateApplicationCommand());
System.out.println(“in myController:: after sending CreateApplicationCommand”);
}
}

This command is being handled in the Aggregate, The Aggregate class is annotated with org.axonframework.spring.stereotype.Aggregate:

@Aggregate
class MyAggregate{
@CommandHandler //org.axonframework.commandhandling.CommandHandler
private MyAggregate(CreateApplicationCommand command) {
org.axonframework.modelling.command.AggregateLifecycle.apply(new AppCreatedEvent());
System.out.println(“in MyAggregate:: after firing AppCreatedEvent”);
}

@EventSourcingHandler //org.axonframework.eventsourcing.EventSourcingHandler
private void on(AppCreatedEvent appCreatedEvent) {
// Updates the state of the aggregate
this.id = appCreatedEvent.getId();
this.name = appCreatedEvent.getName();
System.out.println(“in MyAggregate:: after updating state”);
}
}

The AppCreatedEvent is handled at 2 places:

  1. In the Aggregate itself, as we can see above.
  2. In the projection class as below:

    @EventHandler //org.axonframework.eventhandling.EventHandler
    void on(AppCreatedEvent appCreatedEvent){
    // persists into database
    System.out.println(“in Projection:: after saving into database”);
    }

The problem here is after catching the event at first place(i.e., inside aggregate) the call gets returned to myController.
i.e. The output here is:

in MyAggregate:: after firing AppCreatedEvent
in MyAggregate:: after updating state
in myController:: after sending CreateApplicationCommand
in Projection:: after saving into database

The output which i want is:

in MyAggregate:: after firing AppCreatedEvent
in MyAggregate:: after updating state
in Projection:: after saving into database
in myController:: after sending CreateApplicationCommand

In simple words, i want axon to wait untill all events triggered against a particular command are executed completely and then return to the class which triggered the command.

After searching on the forum i got to know that all sendAndWait does is wait until the handling of the command and publication of the events is finalized, and then i tired with Reactor Extension as well using below but got same results: org.axonframework.extensions.reactor.commandhandling.gateway.ReactorCommandGateway.send(new CreateApplicationCommand()).block();

Can someone please help me out.
Thanks in advance.

Hi Rohit, I believe @Steven_van_Beelen already answered you on StackOverFlow here. Is it enough for you?

If not, we can proceed the questions, either here or there.

Hi Rohit,

the answer by @Steven_van_Beelen is exactly what yu want. I’m building a small lib that contain such helper gateways right now… So there is already an issue on it: https://github.com/holixon/axon-gateway-extension/issues/5

Hi @Simon_Zambrovski and @Steven_van_Beelen ,

Sorry for the late reply. But i must say thanks for the amazing support. i am able to implement my requirement using subscription queries and axon-reactor-gateway as suggesed by steven.

Thanks a lot. :slight_smile:

2 Likes