Need bit clarification on sendAndWait() command

Hello,

I am a newbie in axon framework, I am a bit confused about sendAndWait command.

First :
From the docs :

Sends the given command and wait for it to execute. The result of the execution is returned when available. This method will block indefinitely, until a result is available, or until the Thread is interrupted. When the thread is interrupted, this method returns null. If command execution resulted in an exception, it is wrapped in a org.axonframework.commandhandling.CommandExecutionException

So is the caller thread blocked or the thread spawned after the sendAndWait is fired is blocked.For example if i am calling the sendAndWait() command from main will main thread be blocked ? And if it is the case then

class main (String args[])

{

commandGateway.sendAndWait(new Command());

System.out.println(“Thread not blocked”);

}

“Thread not blocked” should not be logged.

Second:

The sendAndWait() command returns the result for the execution of the command.

From the docs:

The result of the execution is returned when available.

and in the commandHandler we write :

@CommandHandler
public void acknowledge(AcknowledgedCommand acknowledgedCommand) {
apply(new AcknowledgedEvent(
acknowledgedCommand.getA(), acknowledgedCommand.getB()));
}

The apply command return’s void then how the command execution result is returned.Even if do some modification in the eventHandler like returning value from the event handler (which is not recommended but done only for the purpose of testing)

@SagaEventHandler(associationProperty = “a”)
public String handle(StateEvent stateEvent) {
return this.state = stateEvent.getState();
}

The event would not return back anything on the commandHandler. As the flow does not go back again to the commandHandler after execution of the event (until and unless command is published from the event ). Then what is actually returned from the execution ?

If anyone can clarify it would be great.

Thanks,

Please ignore the First point of sendAndWait thread blocking , as i was using async saga it was firing the command and returning back.so the log statement was printed.

Hi,

the return value is the value returned by the @CommandHandler method. The event handlers don’t play any role in this. Just imagine having dozens of listeners for an event applied by a command; what would be the return value?

SendAndWait will block the sending thread until the command has been executed and its result has been reported.

cheers,

Allard