Search code examples
springaxon

Is it good idea call third party services from axon aggregate


I have an axon aggregate. It handle command and before applying event has to invoke third party service for validation some parameters, according to this validation i apply events or not. Is it good practice? Or I have make validation before i send command?

@Aggregate
public class SomeAggregate {
[...]
   @CommandHandler
   public void someHandler() {
     if(thirdPartyService.invoke) {
       apply(...)
     }
   }
}

Solution

  • If it's a non-blocking (domain) service, something like a finite state machine, it's okay to call from within the aggregate, since it's most likely going to finish soon. However, 'third party service' to me sounds like an outbound call, which might take some time.

    When Axon loads an aggregate, it blocks the aggregate so no other thread can change it's state/handle commands on it. A third-party service would mean that the aggregate is blocked even longer.

    Hence, I would suggest not calling a third party service in your aggregate. Either call the service prior to entering the aggregate or perform a compensating action after command handling was finalized to revert the decision. Which of the two makes most sense in your scenario, is dependent on your domain. I see "pre-validation" through the third-party service however as the most reasonable option to take.