2

我有一个轴突聚集体。它处理命令并且在应用事件之前必须调用第三方服务来验证一些参数,根据这个验证我是否应用事件。这是好习惯吗?或者我在发送命令之前进行了验证?

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

2 回答 2

6

如果它是非阻塞(域)服务,比如有限状态机,则可以从聚合中调用,因为它很可能很快就会完成。但是,对我来说,“第三方服务”听起来像是一个呼出电话,可能需要一些时间。

当 Axon 加载聚合时,它会阻塞聚合,因此没有其他线程可以更改它的状态/处理命令。第三方服务将意味着聚合被阻止的时间更长。

因此,我建议不要在您的聚合中调用第三方服务。要么在输入聚合之前调用服务,要么在命令处理完成后执行补偿操作以恢复决策。两者中哪一个在您的场景中最有意义,取决于您的域。我认为通过第三方服务进行“预验证”是最合理的选择。

于 2017-06-28T06:57:04.597 回答
0

这取决于。如果您的第三方服务有副作用并且不是幂等的,那么我不确定该怎么做(我仍在努力解决)。

如果它确实有副作用,那么我希望聚合阻止/锁定并使用聚合的状态/历史来仔细管理这样的交互

@Aggregate
public class SomeAggregate {
[...]
   @CommandHandler
   public void someHandler() {
    /*
        Reason about whether its appropriate to send a request. 
        e.g. if a request has been sent but no response has been received,
        then depending on the third party service it might be in an indeterminate state.
        Instead of trying to interact with it, it might be better 
        to notify someone instead. 
    */
    rejectIfNotSafe()      

     /*
         Effectively locks this interaction / any other instances in the same path
         should get a concurrent modification exception when trying to commit this event.
     */
     commit(new ThirdPartyServiceRequested())

     if(thirdPartyService.invoke) {
       apply(...)
     }
   }
}

但是 Axon 的“工作单元”意味着发出的事件在命令处理程序完成之前不会被发布/提交,所以我们不能防止这种方式。

有任何想法吗?

于 2019-11-13T23:11:28.387 回答