Spring @Scheduled with cron,如果服务在预定日期宕机,我该如何处理

问题描述

所以假设我的系统要求您在开始使用之前添加一些余额。因此,假设它会在每个月的第 30 天扣除/支出您的余额。所以我将 AxonFramework 与 spring Scheduled annotation 结合使用,所以代码可能看起来像这样。

@CommandHandler
@Scheduled(cron = "** cron for every 30th **")
fun handle(command: ExpendNetflixUserCommand){
  if (user.balance < netflixPackage.price){
    AggregateLifecycle.apply(command.toNetflixUserdisabled())
    throw IllegalArgumentException("Insufficient balance,please add.")
  }
  AggregateLifecycle.apply(command.toNetflixUserExpended())
}

所以我的问题是,如果是在 30 日,我的系统即将计算这些付款,但突然服务中断/中断,它会在 31 日重新开始工作,它会重新计算以前的付款吗?所以如果没有,你能建议我应该如何处理这个案子。

解决方法

对于这个用例,我可能有一个带有 @Schedule 注释的外部组件,它将使用 CommandGateway 的实例来分派命令! 使用这种方法,这个组件在您的代码之外并带来额外的好处。 其中之一是在此 RetryScheduler 上配置 CommandGateway 的能力,它会根据需要重试失败的命令!

查看参考指南以获取有关如何执行此操作以及 Axon 为您提供开箱即用的实现方式的更多信息! https://docs.axoniq.io/reference-guide/axon-framework/axon-framework-commands/implementations#configuring-the-command-gateway

,

您可能应该考虑依赖更成熟的解决方案,例如 Spring Batch,因为您几乎可以立即获得工作状态管理之类的功能。